Hi!
I am trying to initiate a client-server talk but for some reason it is not working. The server starts running, but when I start the client nothing happens. Please point out the mistake in my code-
Server-
import java.io.*;
import java.net.*;
public class MyServer
{
public static void main(String[] args) throws Throwable {
//Listen for incoming Connections
ServerSocket s1=new ServerSocket(12345);
System.out.println("Server Running");
Socket s;
int i=1;
String op;
while(true)
{
s=s1.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream());
out.println("Hello Client");
while(in.readLine()!=null)
{
//Conversation is still on
op="Arbit Line - Number "+i;
out.println(op);
i++;
if(i==5)
{
out.println("Bye");
break;
}
}
}
}
}
Client-
import java.io.*;
import java.net.*;
public class MyClient
{
public static void main(String[] args) {
try
{
Socket mySocket=new Socket("127.0.0.1",12345);
//If successful, we have the socket running
//Set up read and write
BufferedReader in=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
PrintWriter out=new PrintWriter(mySocket.getOutputStream());
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer,fromUser,op;
int i=1;
while((fromServer = in.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
//Conversation is still on
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}