这是我的服务器代码:
package socketProject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class SocServer {
public static void main(String[] args) throws Exception
{
System.out.println("Server is started");
ServerSocket ss = new ServerSocket(9999);
System.out.println("Server is waiting for the client response");
Socket s = ss.accept();
System.out.println("Client is connected");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.println("Client Data:" + str);
}
}
这是我的客户代码:
package socketProject;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class SocClient {
public static void main(String[] args) throws Exception
{
String ip="localhost";
int port =9999;
Socket s = new Socket(ip,port);
String str ="Nemi Bhattarai";
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
PrintWriter out = new PrintWriter(os);
out.write(str);
out.flush();
}
}
每当我尝试运行此操作时,都会出现以下错误:
线程“main”中的异常java.net.SocketException:连接重置 在java.net.SocketInputStream.read(未知来源)at java.net.SocketInputStream.read(未知来源)at sun.nio.cs.StreamDecoder.readBytes(未知来源)at sun.nio.cs.StreamDecoder.implRead(未知来源)at sun.nio.cs.StreamDecoder.read(未知来源)at java.io.InputStreamReader.read(未知来源)at java.io.BufferedReader.fill(未知来源)at java.io.BufferedReader.readLine(未知来源)at java.io.BufferedReader.readLine(未知来源)at socketProject.SocServer.main(SocServer.java:22)
我做错了什么?