Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
groovy和java程序之间通过套接字的通信_Java_Sockets_Groovy_Client - Fatal编程技术网

groovy和java程序之间通过套接字的通信

groovy和java程序之间通过套接字的通信,java,sockets,groovy,client,Java,Sockets,Groovy,Client,我正试图用groovy编写一个客户端和Java服务器端的小型套接字程序。下面是我写的代码 客户: def s = new Socket("localhost", 4444); s << "Server before withStreams\n"; s.withStreams { input, output -> println"Sending message1" output << "server message1\n" } s.close(); 当我执

我正试图用groovy编写一个客户端和Java服务器端的小型套接字程序。下面是我写的代码

客户:

def s = new Socket("localhost", 4444);
s << "Server before withStreams\n";
s.withStreams { input, output ->
  println"Sending message1" 
  output << "server message1\n"
}
s.close();
当我执行这两个程序时,套接字通信就建立起来了。但是当服务器代码从socket message=in.readLine读取时,我在服务器代码中得到一个IOException


我想在客户端的socket中写入时存在一些格式问题。但却无法找出确切的问题。有人能帮忙吗?

您通常不想关闭每个客户端连接的ServetSocket。您希望这样做一次或每次启动服务器时,然后在每次接受时处理客户机连接并关闭该连接的套接字,但保持ServerSocket打开,直到您想要停止服务器

这里是示例服务器的重写版本,它还为每个客户端请求创建一个新线程来处理多个并发请求。请注意,由于测试客户端不通过发送终止字符串,因此连接和套接字保持打开状态

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

public class Logger {
    private ServerSocket providerSocket;

    Logger() {

    }

    public void start() {
        try {
            providerSocket = new ServerSocket(4444, 10);
            while (true) {
                System.out.println("Waiting for connection");
                Socket connection = providerSocket.accept();
                new Thread(new Job(connection)).start();
            }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (providerSocket != null) {
            System.out.println("Stopping server");
            try {
                providerSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }

    static class Job implements Runnable {
        final Socket connection;
        private static int id;
        private int clientId = ++id;

        public Job(Socket connection) {
            this.connection = connection;
        }

        public void run() {
            BufferedReader in = null;
            try {
                System.out.println("Connection " + clientId + " received from " + connection.getInetAddress().getHostName());
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String message = "InitialMessage";
                do {
                    if (in.ready()) {
                        try {
                            // not sure why want to read one character then read the line
                            //int ch = in.read();
                            //System.out.println(ch);
                            // -1 if the end of the stream has been reached
                            //if (ch == -1) break;
                            message = in.readLine();
                            // null if the end of the stream has been reached
                            if (message == null) break;
                            System.out.println("client>" + message);
                        } catch (IOException e) {
                            System.out.println(e);
                            e.printStackTrace();
                            break;
                        }
                    }
                } while (!message.equals("bye"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //4: Closing connection
                System.out.println("Close connection " + clientId);
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String args[]) {
        Logger server = new Logger();
        server.start();
    }
}
import java.io.*;
import java.net.*;

public class Logger {
    private ServerSocket providerSocket;

    Logger() {

    }

    public void start() {
        try {
            providerSocket = new ServerSocket(4444, 10);
            while (true) {
                System.out.println("Waiting for connection");
                Socket connection = providerSocket.accept();
                new Thread(new Job(connection)).start();
            }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (providerSocket != null) {
            System.out.println("Stopping server");
            try {
                providerSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }

    static class Job implements Runnable {
        final Socket connection;
        private static int id;
        private int clientId = ++id;

        public Job(Socket connection) {
            this.connection = connection;
        }

        public void run() {
            BufferedReader in = null;
            try {
                System.out.println("Connection " + clientId + " received from " + connection.getInetAddress().getHostName());
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String message = "InitialMessage";
                do {
                    if (in.ready()) {
                        try {
                            // not sure why want to read one character then read the line
                            //int ch = in.read();
                            //System.out.println(ch);
                            // -1 if the end of the stream has been reached
                            //if (ch == -1) break;
                            message = in.readLine();
                            // null if the end of the stream has been reached
                            if (message == null) break;
                            System.out.println("client>" + message);
                        } catch (IOException e) {
                            System.out.println(e);
                            e.printStackTrace();
                            break;
                        }
                    }
                } while (!message.equals("bye"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //4: Closing connection
                System.out.println("Close connection " + clientId);
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String args[]) {
        Logger server = new Logger();
        server.start();
    }
}