小弟最近练习Java socket tcp这部分.出了点问题。客户端给服务器发送信息。第一次服务器可以收到,但是客户端第二次发送,就报异常了:Exception in thread "main" java.net.SocketException: Connection reset by peer: socket write error
具体代码如下:Server.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;public class Server {
private ServerSocket sskt=null;
public static final int port=9999;
public ServerSocket getSskt() {
return sskt;
} public Server() {
try {
sskt=new ServerSocket(9999);
System.out.println("服务器启动成功..."+"端口号:"+port);
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket acceptSkt() throws IOException{
Socket skt=null;
skt=getSskt().accept();
System.out.println("一个客户端程序已连接."+"clientInfo:"+skt.getInetAddress().getHostAddress()+"-->"+skt.getPort());
return skt;
}
public String readMsg(Socket skt) throws IOException{
InputStream ins=skt.getInputStream();
DataInputStream dis=new DataInputStream(ins);
String msg= dis.readUTF();
System.out.println("客户端发来的信息:"+msg);
return msg;
}
public static void main(String[] args) throws IOException{
Server server=new Server();
Socket skt= server.acceptSkt();
String msg=server.readMsg(skt);
}}client.java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;public class Client {
public static final String host = "127.0.0.1";
public static final int port = 9999;
private Socket skt = null;
private DataOutputStream dos = null; public DataOutputStream getDos() {
return dos;
} public Client() {
try {
skt = new Socket(host, port);
dos = new DataOutputStream(skt.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } public String getMsgFrmConsole() throws IOException {
System.out.print("请输入:");
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
System.in));
String msg = null;
msg = bufReader.readLine();
return msg;
} public void sendmsg(String msg) throws IOException { getDos().writeUTF(msg);
getDos().flush(); } public String reviceMsg() throws IOException {
return null;
} public static void main(String[] args) throws UnknownHostException,
IOException { Client client = new Client();
while (true) {
String msg = client.getMsgFrmConsole();
client.sendmsg(msg);
} }}
在此感谢大家的帮助了
java net socket 异常
具体代码如下:Server.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;public class Server {
private ServerSocket sskt=null;
public static final int port=9999;
public ServerSocket getSskt() {
return sskt;
} public Server() {
try {
sskt=new ServerSocket(9999);
System.out.println("服务器启动成功..."+"端口号:"+port);
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket acceptSkt() throws IOException{
Socket skt=null;
skt=getSskt().accept();
System.out.println("一个客户端程序已连接."+"clientInfo:"+skt.getInetAddress().getHostAddress()+"-->"+skt.getPort());
return skt;
}
public String readMsg(Socket skt) throws IOException{
InputStream ins=skt.getInputStream();
DataInputStream dis=new DataInputStream(ins);
String msg= dis.readUTF();
System.out.println("客户端发来的信息:"+msg);
return msg;
}
public static void main(String[] args) throws IOException{
Server server=new Server();
Socket skt= server.acceptSkt();
String msg=server.readMsg(skt);
}}client.java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;public class Client {
public static final String host = "127.0.0.1";
public static final int port = 9999;
private Socket skt = null;
private DataOutputStream dos = null; public DataOutputStream getDos() {
return dos;
} public Client() {
try {
skt = new Socket(host, port);
dos = new DataOutputStream(skt.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } public String getMsgFrmConsole() throws IOException {
System.out.print("请输入:");
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
System.in));
String msg = null;
msg = bufReader.readLine();
return msg;
} public void sendmsg(String msg) throws IOException { getDos().writeUTF(msg);
getDos().flush(); } public String reviceMsg() throws IOException {
return null;
} public static void main(String[] args) throws UnknownHostException,
IOException { Client client = new Client();
while (true) {
String msg = client.getMsgFrmConsole();
client.sendmsg(msg);
} }}
在此感谢大家的帮助了
java net socket 异常
解决方案 »
- 按章节分割文件,涉及到正则表达式
- js 放大镜效果
- String s="a"+"b"+"c"到底产生了几个对象?
- 请教一个Java程序.
- 请问public static <T> void sort(T[] a, Comparator<? super T> c) 中 <T> 和<? super T> 什么意思?先谢了!
- 请问如固定JFram 窗口的大小,就是让最大化按钮不可用.
- 二进制流在服务器/客户之间传输文件???
- 我是一个新手
- 谁有使用正则表达式的例子。
- 求助:如何在Java程序中启动系统的浏览器?谢谢了!!!
- 求高手点拨“im聊天工具登录功能”的代码错误原因
- Java Swing JTable 如何做到动态灰化
while(true){
String msg=server.readMsg(skt);
if(msg.equals("bye")){
System.out.println("客户端退出!");
break;
}
}