Java套接字发送数据

时间:2016-03-20 11:51:28

标签: java sockets

我创建了两个类一个服务器和客户端,我通过套接字输入和输出流发送数据但是不能发送多个消息?

服务器:

public class SOK_1_SERVER {

    public static void main(String[] args) throws Exception
    {

        SOK_1_SERVER Sever = new SOK_1_SERVER();
        Sever.run();
    }

    private void run() throws Exception {
        ServerSocket SRVSOCK = new ServerSocket(444);
        //Waits both client and server to accept and we return 
        //a socket
        Socket SOCK = SRVSOCK.accept();
        //Once accepted 
        InputStreamReader isr = new InputStreamReader(SOCK.getInputStream());
        BufferedReader br = new BufferedReader(isr);

        String message = br.readLine();
        System.out.println("I read: " + message + "from Client");

        if(message != null)
        {
            //Sending message back to client 
            PrintStream ps = new PrintStream(SOCK.getOutputStream());
            ps.println("Message Received");
            ps.println("Send from Server");
        }
    }
}

客户

public class SOK_1_CLIENT {

    public static void main(String[]args) throws Exception
    {
        SOK_1_CLIENT client = new SOK_1_CLIENT();
        client.run();
    }

    private void run() throws Exception {
        Socket SOCK = new Socket("localhost",444);
        PrintStream ps = new PrintStream(SOCK.getOutputStream());
        ps.println("Hello to Server from client");

        InputStreamReader ir = new InputStreamReader(SOCK.getInputStream());
        BufferedReader br = new BufferedReader(ir);

        String message = br.readLine();

        System.out.println(message);

    }

}

它只输出收到的消息,但我认为它是因为我需要有一个循环来继续检查新消息,但不确定,因为我刚刚开始查看套接字。

1 个答案:

答案 0 :(得分:1)

在您的客户端类中,您需要迭代bufferedReader。

而不是

Team

使用此,

SELECT  Team
FROM    club
WHERE   TeamCode IN (SELECT DISTINCT    
HomeTeam and Awayteam FROM matches);