服务器端代码:
ServerSocket server=null;
try
{
server=new ServerSocket(5432);
}
catch(IOException e )
{
}
OutputStream os;
InputStream is;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos;
DataInputStream dis;
Socket s;
InetAddress address;
String receiveMsg;
String sendMsg;
while(true)
{
try
{
s=server.accept();
address=s.getInetAddress();
System.out.println(address+"已连接");
os=s.getOutputStream();
is=s.getInputStream();
dos=new DataOutputStream(os);
dis=new DataInputStream(is);
receiveMsg=new String(dis.readUTF());
System.out.println("The message from client is:"+receiveMsg);
System.out.println("What is the message that we will send to the client:");
sendMsg=br.readLine();
dos.writeUTF(sendMsg);
dos.flush();
}
catch(IOException e)
{
e.printStackTrace();
}客户端代码;
Socket s;
OutputStream os;
InputStream is;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos;
DataInputStream dis;
String reciveMsg=null;
String sendMsg=null;
try
{
s=new Socket("127.0.0.1",5432);
is=s.getInputStream();
os=s.getOutputStream();
dos=new DataOutputStream(os);
dis=new DataInputStream(is);
System.out.println("Please input the message that will send to the server: ");
sendMsg=br.readLine();
dos.writeUTF(sendMsg);
reciveMsg=dis.readUTF();
System.out.println("The message from the server is:"+reciveMsg);
}
catch(IOException e)
{
}运行会出现异常, receiveMsg=new String(dis.readUTF());就是这句。java.net.SocketException: Connection reset
另外,我用的NetBeans6.0,生成的jar文件运行时没有仿dos界面,看不见想看的东西,能不能调啊
ServerSocket server=null;
try
{
server=new ServerSocket(5432);
}
catch(IOException e )
{
}
OutputStream os;
InputStream is;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos;
DataInputStream dis;
Socket s;
InetAddress address;
String receiveMsg;
String sendMsg;
while(true)
{
try
{
s=server.accept();
address=s.getInetAddress();
System.out.println(address+"已连接");
os=s.getOutputStream();
is=s.getInputStream();
dos=new DataOutputStream(os);
dis=new DataInputStream(is);
receiveMsg=new String(dis.readUTF());
System.out.println("The message from client is:"+receiveMsg);
System.out.println("What is the message that we will send to the client:");
sendMsg=br.readLine();
dos.writeUTF(sendMsg);
dos.flush();
}
catch(IOException e)
{
e.printStackTrace();
}客户端代码;
Socket s;
OutputStream os;
InputStream is;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos;
DataInputStream dis;
String reciveMsg=null;
String sendMsg=null;
try
{
s=new Socket("127.0.0.1",5432);
is=s.getInputStream();
os=s.getOutputStream();
dos=new DataOutputStream(os);
dis=new DataInputStream(is);
System.out.println("Please input the message that will send to the server: ");
sendMsg=br.readLine();
dos.writeUTF(sendMsg);
reciveMsg=dis.readUTF();
System.out.println("The message from the server is:"+reciveMsg);
}
catch(IOException e)
{
}运行会出现异常, receiveMsg=new String(dis.readUTF());就是这句。java.net.SocketException: Connection reset
另外,我用的NetBeans6.0,生成的jar文件运行时没有仿dos界面,看不见想看的东西,能不能调啊
有可能是在客户端还没有读完毕数据,服务器端就把线路断开了,所以收数据失败。NetBeans6.0没有用过,但是jar一定会有打印输出的,这个和什么生成的没有关系。
你在cmd下用java -jar ×××.jar 来运行看看。