当前位置: 技术问答>java相关
请帮忙看一看client/server模式通信的问题
来源: 互联网 发布时间:2015-05-26
本文导语: 服务器端: import java.net.*; import java.io.*; public class dc extends Thread { public dc() { } public static void main(String[] args) { while(true){ ServerSocket ssock=null; Socket incomingsock=null; try{ s...
服务器端:
import java.net.*;
import java.io.*;
public class dc extends Thread {
public dc() {
}
public static void main(String[] args)
{
while(true){
ServerSocket ssock=null;
Socket incomingsock=null;
try{
ssock=new ServerSocket(8000);
incomingsock=ssock.accept();
}
catch(IOException e){System.out.println(e.getMessage());}
PrintWriter pw=null;
BufferedReader br=null;
String msg=null;
msg="1234112345678912";//00000000helloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyou";
try{
pw=new PrintWriter(incomingsock.getOutputStream());
pw.print(msg);}
catch(IOException e){System.out.println(e.getMessage());}
}
}}
客户机端:
import java.net.*;
import java.io.*;
public class client extends Thread {
public client() {
}
public static void main(String[] args)
{
Socket sock=null;
try{
sock=new Socket("127.0.0.1",8000);
BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream()));
String msg= br.readLine();
System.out.println(msg);
}catch(SocketException e){System.out.println(e.toString());}
catch(IOException e){System.out.println(e.toString());}
finally{
try{sock.close();}catch(IOException e){System.out.println(e.toString());}
}
}
}
客户端给出的错误是:
java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read
请高手指教
import java.net.*;
import java.io.*;
public class dc extends Thread {
public dc() {
}
public static void main(String[] args)
{
while(true){
ServerSocket ssock=null;
Socket incomingsock=null;
try{
ssock=new ServerSocket(8000);
incomingsock=ssock.accept();
}
catch(IOException e){System.out.println(e.getMessage());}
PrintWriter pw=null;
BufferedReader br=null;
String msg=null;
msg="1234112345678912";//00000000helloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyouhelloworldyou";
try{
pw=new PrintWriter(incomingsock.getOutputStream());
pw.print(msg);}
catch(IOException e){System.out.println(e.getMessage());}
}
}}
客户机端:
import java.net.*;
import java.io.*;
public class client extends Thread {
public client() {
}
public static void main(String[] args)
{
Socket sock=null;
try{
sock=new Socket("127.0.0.1",8000);
BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream()));
String msg= br.readLine();
System.out.println(msg);
}catch(SocketException e){System.out.println(e.toString());}
catch(IOException e){System.out.println(e.toString());}
finally{
try{sock.close();}catch(IOException e){System.out.println(e.toString());}
}
}
}
客户端给出的错误是:
java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read
请高手指教
|
这个程序有几个问题:
1、服务端在接收请求时因判断接收到的是否可用;
2、在pw.write(msg);之后应加入以下内容:
pw.flush();
pw.close();
个人意见,希望能对你有所帮助。
1、服务端在接收请求时因判断接收到的是否可用;
2、在pw.write(msg);之后应加入以下内容:
pw.flush();
pw.close();
个人意见,希望能对你有所帮助。
|
Look : it is my server/client
good luck!
////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.io.PrintStream;
import java.lang.*;
public class MyServer {
ServerSocket sSocket = null;
public void MyServer(){
}
public static void main(String args[]) {
MyServer ms = new MyServer();
ms.start();
}
public void start(){
try{
sSocket = new ServerSocket(8000);
System.out.println(port);
while(true){
Socket sin = sSocket.accept();
//System.out.println(sin.getLocalAddress());
//System.out.println(sin.getInetAddress());
i++;
//if (i>99999999) i = 1;
//ThreadProc tp = new ThreadProc(sin,i);
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
endServer();
}catch(Exception ex){}
}
}
public void endServer(){
try{
sSocket.close();
System.exit(0);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
//////////////////////////////////////////
good luck!
////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.io.PrintStream;
import java.lang.*;
public class MyServer {
ServerSocket sSocket = null;
public void MyServer(){
}
public static void main(String args[]) {
MyServer ms = new MyServer();
ms.start();
}
public void start(){
try{
sSocket = new ServerSocket(8000);
System.out.println(port);
while(true){
Socket sin = sSocket.accept();
//System.out.println(sin.getLocalAddress());
//System.out.println(sin.getInetAddress());
i++;
//if (i>99999999) i = 1;
//ThreadProc tp = new ThreadProc(sin,i);
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
endServer();
}catch(Exception ex){}
}
}
public void endServer(){
try{
sSocket.close();
System.exit(0);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
//////////////////////////////////////////