こんにちは、基本的にクライアントから「Hello」メッセージのように送信し、サーバーが取得して出力する単純なソケットプログラムを作成しようとしています。
私はこのガイドに従おうとしています: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4
ただし、ポート番号でserverSocketをインスタンス化しようとすると、引数を削除するか、そのメソッドの新しいコンストラクターを作成するようにアドバイスする構文エラーが発生します。また、accept() メソッドを使用しようとしても認識しません。なぜこれが起こっているのか知っている人はいますか?
ここに私のクライアントコードがあります:
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket testSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
try
{
testSocket = new Socket("192.168.0.104", 5932);
os = new DataOutputStream(testSocket.getOutputStream());
is = new DataInputStream(testSocket.getInputStream());
}
catch (UnknownHostException e)
{
System.err.println("Couldn't find Host");
}
catch (IOException e)
{
System.err.println("Couldn't get I/O connection");
}
if (testSocket != null && os != null && is != null)
{
try
{
os.writeBytes("Hello Server!\n");
os.close();
is.close();
testSocket.close();
}
catch (UnknownHostException e)
{
System.err.println("Host not found");
}
catch (IOException e)
{
System.err.println("I/O Error");
}
}
}
これが私のサーバーコードです(更新済み):
public static void main(String[] args)
{
String line = new String() ;
try
{
ServerSocket echoServer = new ServerSocket(5932);
Socket clientSocket = echoServer.accept();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
PrintStream os = new PrintStream(clientSocket.getOutputStream());
while (true) {
line = is.readLine();
os.println(line);
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
こちらが実際のスクリーンショットです。
エラー1: http://i.imgur.com/8JIOd.png エラー2: http://i.imgur.com/7uCow.png