简体   繁体   中英

Message not sending to Server from Client - Java

I have a Server-Client program where I send a small messsage to the client using JLabel . When that message is recieved from server that particular client must send a response immediately. But it is not sending any message . Can somebody look at my code and tell me where my mistake is?

//SERVER

void connect_clients()
    {
        try {
            ServerSocket listener = new ServerSocket(7700);
            jButton1.setText("Server Running!");
            jButton1.setEnabled(false);
                while (true) {

                    socket = listener.accept();
                    socketList.add(socket);
                    //socketList.add(listener.accept());
                     BufferedReader ed = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String tmp = ed.readLine();
             System.out.print("I Recieved :"+tmp);

                }

            }
        catch(IOException ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }
    }

//CLIENT

 void connect_server() throws IOException
    {
        try {
            // TODO code application logic here
            String serverAddress = JOptionPane.showInputDialog(
                    "Enter IP Address of a machine that is\n" +
                            "running the date service on port 9090:");
            s = new Socket(serverAddress, 7700);

            while(true){
            BufferedReader input =
            new BufferedReader(new InputStreamReader(s.getInputStream()));
            String answer = input.readLine();
            System.out.println(answer);
                if(answer != null)
                {
                    PrintStream pr = new PrintStream(s.getOutputStream());
                    InputStreamReader rd = new InputStreamReader(System.in);
                    BufferedReader ed = new BufferedReader(rd);
                    String temp = ed.readLine();
                    pr.println(temp);

                    JOptionPane.showMessageDialog(null,"Answer is not null");  //THIS WORKS
               }

            }

          }
       catch (ConnectException e) {

            JOptionPane.showMessageDialog(null, e);
        }
         catch (SocketException e) {

            JOptionPane.showMessageDialog(null, e);
        }
    }

Some points that you missed in your implementation:

  • the streams and sockets are never closed
  • in the client i do not see the point of the endless loop
  • the client should initialize the communication by sending a message via output stream (not to try to read first)

For a simple example the steps should be:

  1. Start sever to listen and once a connection is established to read the message (you did)
  2. The client should sent a message via output stream and close the steams and the socket
  3. The severs should close the streams and the sockect for the established connection

Example:

//Server
socket = listener.accept();
BufferedReader ed = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter pr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream());
String tmp = ed.readLine();
System.out.print("I Recieved :"+tmp);
String msg = "Message received";
pr.write(msg,0,msg.length());
pr.newLine();
ed.close();
pr.close();
socket.close();

//Client
BufferedWriter pr = new BufferedWriter(new OutputStreamWriter(s.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String sendMessage = "Send Message";
pr.write(msg,0,msg.length());
pr.newLine();
String answer = input.readLine();
System.out.println(answer);
JOptionPane.showMessageDialog(null,"Answer is not null"); 
input.close();
pr.close();
s.close();

UPDATE

reading from input stream continuously:

BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while((line=input.readLine())!=null){ 
  //do something with line
}

I will suggest a simple approach where server is sending the hi msg to client.

For server:

//Server
ServerSocket ss=new ServerSocket(3554);
socket = ss.accept();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getOutputStream()));
String msg ="Hi from server"
bw.write(msg);
String msgFromClient=br.readLine();
System.out.println(msgFromClient);
bw.close();
socket.close();

For Client:

//Client
Socket socket=new Socket("localhost",3554)
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 
String received = input.readLine();
System.out.println(received);
bw.write("Client recieve :"+received);
br.close();
bw.close();
socket.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM