Java Client/Server reading and writing doesn't work and no error

余生颓废 提交于 2020-01-06 07:51:07

问题


Client.java

package Client;

import java.io.*;
import java.net.*;

class Client {
        /*
     To send string to server use "out.print(data)"
     To use info sent from server use "in.readLine()"
     */
    int port = 1234;
    String hostname = "localhost";
    String input,output;

    public void send(String text) {
    try {
         Socket skt = new Socket(hostname, port);           /*Connects to server*/

         BufferedReader in = new BufferedReader(new
         InputStreamReader(skt.getInputStream()));          /*Reads from server*/
         System.out.println("Server:" + in.readLine());

         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         out.print(text);                                   /*Writes to server*/

         skt.close();
         out.close();    /*Closes all*/
         in.close();
      }

      catch(Exception e) {
         System.out.print("Error Connecting to Server\n");
      } 
    }
   public static void main(String args[]) {
     Client c = new Client();
     c.send("Server is online");    //sends message to server
   }
}

Server.java

package Server;

import java.io.*;
import java.net.*;

class Server {
    /*
     To send string to client use "out.print(data)"
     To use info sent from client use "in.readLine()"
     */
        int port = 1234;
    String input,output;

    public void send(String text) {
    try {
         ServerSocket srvr = new ServerSocket(port);
         Socket skt = srvr.accept();                    /*Waiting for Connection from client*/
         System.out.print("Server has connected!\n");

         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         out.print(text);                                                  /*Write/Send to Client*/

          BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));                   /*Read from Client*/
         System.out.println("Client:" + in.readLine());

         out.close();
         in.close();
         skt.close();               /*Closes all*/
         srvr.close();
      } catch( Exception e) {
         System.out.print("Error Connecting\n");
      } 
    }

   public static void main(String args[]) {
       Server s = new Server();
       s.send("Client is online");  //sends a message to client

   }
}

why does nothing apart from Server has connected! happen when on both I have sent from the server and client

s.send("X is online");

which should be read on the other side and printed out in the cmd?

(is it because the bufferreader is activated after printwriter and doesn't pick it up? if so how can I fix this?)


回答1:


Your BufferedReader#readLine call will block indefinitely unless you send a newline character. Replace

out.print(text); 

with

out.println(text); 

to match the BufferedReader#readLine calls from both client & server.




回答2:


One possibility - look at the ordering here:

 PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
 out.print(text);

 skt.close();
 out.close(); 

You're closing the socket before you close the writer. That means if the writer has any buffered data, it has no chance to write it. Try switching round the order in which you close things - close the socket as the last thing.

(Normally you'd have these in a finally block, mind you...)

EDIT: And then I noticed that you're not printing a line on the server side. You should make both changes.




回答3:


package client;

import java.io.*;
import java.net.*;

class Client {
        /*
     To send string to server use "out.print(data)"
     To use info sent from server use "in.readLine()"
     */
    int port = 1234;
    String hostname = "localhost";
    String input,output;

    public void send(String text) {
    try {
         Socket skt = new Socket(hostname, port);           /*Connects to server*/

         BufferedReader in = new BufferedReader(new
         InputStreamReader(skt.getInputStream()));          /*Reads from server*/
         System.out.println("Server:" + in.readLine());

         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         out.println(text);                                   /*Writes to server*/
         out.close();    /*Closes all*/
         in.close();
         skt.close();

      }

      catch(Exception e) {
         System.out.print("Error Connecting to Server\n");
      } 
    }
   public static void main(String args[]) {
     Client c = new Client();
     c.send("Server is online");    //sends message to server
   }
}
package server;

import java.io.*;
import java.net.*;

class Server {
    /*
     To send string to client use "out.print(data)"
     To use info sent from client use "in.readLine()"
     */
        int port = 1234;
    String input,output;

    public void send(String text) {
    try {

        ServerSocket srvr = new ServerSocket(port);
         Socket skt = srvr.accept();                    /*Waiting for Connection from client*/
         System.out.println("Server has connected!\n");

         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         out.println(text);                                                  /*Write/Send to Client*/

          BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));                   /*Read from Client*/
         System.out.println("Client:" + in.readLine());

       out.close();
         in.close();
         skt.close();              /*Closes all*/
        srvr.close();
      } catch( Exception e) {
         System.out.print("Error Connecting\n");
      } 
    }

   public static void main(String args[]) {
       Server s = new Server();
       s.send("Client is online");  //sends a message to client

   }
}

Above code is working for me See if you missed anything



来源:https://stackoverflow.com/questions/15721426/java-client-server-reading-and-writing-doesnt-work-and-no-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!