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());
         }
    }

}

Recommended Answers

All 3 Replies

dear i want to know about networking

commented: Thread Hijacking, Make your own thread for your Issues or Google it up -1

> but when I start the client nothing happens

Put a few debugging statements to monitor the entire conversation and post where exactly your program hangs. For e.g. print out a debugging statement to the console when the server receives a client request [i.e. after accept()] and so on. Alternatively, if you are using a good IDE, it should be pretty easy to trace the entire client server communication using the in built debugging capabilities.

Also make sure that when you are using buffered streams, don't forget to flush them after you are done with all the writing since a write on the buffered stream doesn't necessarily mean the data is written to the underlying stream.

commented: thanks! +2

Yeah that was the problem, the code was not flushing the stream.

I flushed the write stream and it started to work, thanks a lot for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.