Java套接字。服务器 - 客户端通信

时间:2017-03-29 16:15:41

标签: java sockets server client

我正在尝试将客户端与gui连接到一个没有gui的服务器。正在进行连接,但我无法看到这两个应用之间的任何消息。 (我应该在客户端获取SERVER,在服务器中获取客户端)

客户端连接代码:

select dateadd(@interval, @duration, @startdate)

(输入和输出在此类Client扩展到的GUI类中定义。定义为“protected BufferedReader input; protected PrintWriter输出;“)

此外,服务器代码:

@Override
public void ClientRunning(){
    try {
       connectToServer();
        setStreams();
        ClientRun();

    }catch(EOFException oefException){
        showMessage("\n Client terminated the connection\n");
    }catch(IOException ioException){
        ioException.printStackTrace();
    }finally{
        close();
    }
}



public void connectToServer() throws IOException{
    showMessage("Attempting Connection... \n");
    connection = new Socket(InetAddress.getByName(serverIP),6789);
    showMessage("Connected to: "+ connection.getInetAddress().getHostName());
}


public void setStreams() throws IOException{
   output = new PrintWriter(connection.getOutputStream(),true);
   output.flush();
   input= new BufferedReader(new InputStreamReader(connection.getInputStream()));

    showMessage("\n Streams are now set. \n");
}

public void close(){
    showMessage("\n closing...");
    try{
      output.close();
      input.close();
      connection.close();

    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}
public void showMessage(final String text){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            cwindow.append(text);
        }
    });
}

public void sendMessage(String message){
    output.write("CLIENT - "+message);
    output.flush();
    showMessage("\nCLIENT - "+message);
}

private void ClientRun() throws IOException{
    String message="CLIENT HERE!";
    sendMessage(message);
    do{
        try{
            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");
    }

连接似乎没问题,所以我不知道什么是错的。任何帮助将不胜感激。

PS:我也在服务器上尝试过PrintWriter,并尝试在流语句中尝试catch,问题仍然存在。

3 个答案:

答案 0 :(得分:2)

您正在使用需要换行符的readLine()。要么为您发送的邮件添加\n,要么不要使用readLine()

答案 1 :(得分:1)

[解决] 正如TedTrippin和Alfie提到的readline(),搞砸了我的代码。在每条消息中添加“\ n”后,一切似乎都顺利进行!我还在客户端代码中将printwriter更改为bufferwritter,并且我还在sendMessage中添加了try-catch函数,因为printwritter似乎也会因某些原因引起一些问题。 无论如何,一切都已完成!多谢你们!

更新后的客户代码:

private void ClientRun() throws IOException{
    String message="CLIENT HERE! \n"; <----added \n here.
    sendMessage(message);
    do{
        try{
            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");
    }

public void sendMessage(String message){
    try{                                 <-----added try-catch statement
       output.write("CLIENT - "+message+"\n"); <----adding "\n" here is super userful, instead of having to add \n in each message.
        output.flush();
        showMessage(message);
    }catch(IOException ioException){
        System.out.println("\n ERROR!");
    }
}

protected BufferedReader input;
protected BufferedWriter output; <----changed printwriter to BufferWriter

更新的服务器代码:

public void ServerRunning() throws IOException{
    String message="SERVER HERE! \n";    <--- added \n here as well.
    sendMessage(message);
    do{
        try{

            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");

}

谢谢你们!

答案 2 :(得分:0)

将此代码与我过去使用套接字编写的代码进行比较后,我发现您使用的是output.write(string)input.readline() - 这些代码混合不好,readline()需要换行符write没有给出一个。

write()替换为println()