无法在java中的tcpserver和tcpclient之间连续发送数据

时间:2014-01-02 09:16:27

标签: java tcp client-server

在服务器接受连接后,我的TCP客户端似乎无法接收从服务器发送的数据。

我让客户端启动连接,我喜欢它以这种方式工作。

loop:
client --> Server;
client <-- Server;

我是Java TCP套接字编程的新手,有人可以告诉我如何让它们正常工作。感谢并提前获得帮助。

TCP客户端代码:

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

class TCPClient
{
    public static void main(String argv[]) throws Exception
    {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        while(true)
        {
            System.out.println("Message:");
            sentence = inFromUser.readLine();
            if(!sentence.equals("exit"))
            {
                outToServer.writeBytes(sentence + '\n');
                modifiedSentence = inFromServer.readLine(); //It does not receive any data from server.
                System.out.println("FROM SERVER: " + modifiedSentence);
            }
            else
            {
                outToServer.writeBytes(sentence + '\n');
                clientSocket.close();
                break;
            }
        }
    }
}

TCP服务器代码:

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

class TCPServer
{
    public static void main(String argv[]) throws Exception
    {
        String clientSentence;
        String serverSentence;
        ServerSocket welcomeSocket = new ServerSocket(6789);
        Socket connectionSocket;
        connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient =
        new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        while(true)
        {
            clientSentence = inFromClient.readLine();
            if(!clientSentence.equals("exit\n"))
            {
                System.out.println("Received: " + clientSentence);
                System.out.println("Message:");
                serverSentence = inFromUser.readLine();
                outToClient.writeBytes(serverSentence); //able to send, but client is not receiving.
            }
            else
            {
                connectionSocket.close();
                welcomeSocket.close();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

尝试刷新数据:

outToClient.writeBytes(serverSentence); //able to send, but client is not receiving.
outToClient.flush();

答案 1 :(得分:0)

outToServer.writeBytes(sentence + '\n');outToServer.flush();

outToClient.writeBytes(serverSentence + '\n');outToClient.flush();

答案 2 :(得分:0)

在客户端中,您在向服务器发送数据后关闭套接字。你需要保持开放。

while(true)
    {
        System.out.println("Message:");
        sentence = inFromUser.readLine();
        if(!sentence.equals("exit"))
        {
            outToServer.writeBytes(sentence + '\n');
            modifiedSentence = inFromServer.readLine(); //It does not receive any data from server.
            System.out.println("FROM SERVER: " + modifiedSentence);
        }
        else
        {
            outToServer.writeBytes(sentence + '\n');
            clientSocket.close(); //socket gets closed. Reopen it on the server side or remove that line
            break;
        }
    }