import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args){
try {
ServerSocket ss=new ServerSocket(8888);
System.out.println("Reading:");
Socket s=ss.accept();
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String info=null;
while((info=br.readLine())!=null){
System.out.println("i am :"+info);
}
s.shutdownInput();
br.close();
isr.close();
is.close();
s.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class client{
public static void main(String args[]){
try {
Socket s=new Socket("localhost",8888);
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("ddfdsaf");
pw.flush();
s.shutdownOutput();
pw.close();
os.close();
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------
两个程序如上。
再别的电脑运行没有错误,但是我的电脑会报错,内容如下: 到底是怎么回事?
Reading:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at TCP_UDP.ABC.main(ABC.java:20)
梦影剑魂
相关分类