a server program that needs some help :)
hello guys ;
i tried to write a simple server to send /receive strings concurrently through the same port .i used a class with thread which implements runnable.but the problem is the class which has runnable thread doesnt recognise socket object that i created in main class.(btw im akind of new at java so the terms i used as talking might be wrong) .i guess it will make sense when u see the program below. if i use different ports for sending and receiving and define new socket objects in each class will solve the problem but i wonder if there s any way to do that through the same port concurently.if i can work it out.id like to share the proper program ( both server and client codes) with u all. pls feel free to ask any question.i hope i could express whats wrong with the program.
best regards...
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.DataInput;
class MyThreaddd implements Runnable{
Thread t;
MyThreaddd () {
t = new Thread(this,"My thread");
t.start();
}
public void run() {
for(;;) {
//to receive date from client
Scanner sc=new Scanner(s.getInputStream());
String gelen=sc.nextLine();
System.out.println("client:"+gelen);
}
}
}
public class javaserverchat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss=new ServerSocket(25);
System.out.println("server is ready.."+InetAddress.getLocalHost());
Socket s=ss.accept();
System.out.println(s.getInetAddress()+" connected");
PrintStream pr=new PrintStream(s.getOutputStream());
pr.println("hello pal im server:) "); //to send string to the client
new MyThreaddd();
for(;;) {
// to send the string we type to the client
DataInputStream di=new DataInputStream(System.in);
try {
String okunan=di.readLine();
pr.println(okunan); //Client'a mesaj
//System.out.println(okunan);
//System.out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//---------------------------------
//s.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("port mesgul");
}
}
}