クライアント側がgroovyで、サーバー側がJavaである小さなソケットプログラムを作成しようとしています。以下は、私が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();
は、サーバー:私は両方のプログラムを実行すると
import java.io.*;
import java.net.*;
public class Logger{
ServerSocket providerSocket;
Socket connection = null;
BufferedReader in;
String message="InitialMessage";
Logger(){}
void run()
{
try{
providerSocket = new ServerSocket(4444, 10);
try{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println("Sleep Interrupted");
}
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
do{
if(in.ready())
{
try{
System.out.println(in.read());
message = in.readLine();
System.out.println("client>" + message);
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
break;
}
}
} while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
public static void main(String args[])
{
Logger server = new Logger();
while(true){
server.run();
}
}
}
、ソケット通信が確立されています。ソケットから読み取るときにIOException
がサーバーコードに入ります(message = in.readLine();
)
クライアントのソケットに書込み時に何らかのフォーマット問題があると思います。しかし、正確な問題を把握することはできません。誰でも助けることができますか?
stacktrace – Anton