Right now I have two client classes I opened my server socket I connected them but the problem is I wanna send a string from one client to another client. Also my clients connect from my local host. But I wanna give them a identical ıp how can I make it. I have to send string from one client to another client by using my server.
public class MainServer {
public static void main(String[] args) throws IOException {
Socket s = null;
ServerSocket ss2 = null;
System.out.println("Server listening...");
ss2 = new ServerSocket(5151);
while (true) {
try {
s = ss2.accept();
System.out.println("Connection started");
ServerThread st = new ServerThread(s);
st.start();
} catch (Exception e) {
System.out.println("Error Occurred");
}
}
}
}
class ServerThread extends Thread {
String line = null;
DataInputStream is = null;
DataInputStream br = null;
PrintWriter os = null;
Socket sl = null;
public ServerThread(Socket s) {
sl = s;
}
public void run() {
try {
is = new DataInputStream(sl.getInputStream());
os = new PrintWriter(sl.getOutputStream());
line = is.readLine();
while (line.compareTo("Quit") != 0) {
os.println(line);
os.flush();
System.out.println("Response of Client:" + line);
line = is.readLine();
}
is.close();
os.close();
sl.close();
} catch (Exception e) {
}
}
}
and here is my client
public class Client2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Socket sl = null;
String line = null;
DataInputStream br = null;
DataInputStream is = null;
PrintWriter os = null;
try {
sl = new Socket("127.0.0.1", 5151);
br = new DataInputStream(System.in);
is = new DataInputStream(sl.getInputStream());
os = new PrintWriter(sl.getOutputStream());
} catch (IOException e) {
System.out.println("IO Exception");
}
System.out.println("Enter data to echo server");
String response = null;
try {
line = br.readLine();
while (line.compareTo("Quit") != 0) {
os.println(line);
os.flush();
response = is.readLine();
System.out.println("Server response:" + response);
line = br.readLine();
}
is.close();
os.close();
br.close();
sl.close();
System.out.println("Connection closed");
} catch (Exception e) {
System.out.println("Socket error");
}
}
}