服务器端:
package p1;
import java.net.*;
import java.io.*;
public class s1 {
public static void main(String[] args)
{
int port =8888;
BufferedReader br;
PrintWriter pw;
ServerSocket ss;
Socket s;
try
{
ss = new ServerSocket(port);
s=ss.accept();
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
System.out.println("wait.....");
while(true)
{
String str=br.readLine();
System.out.println(str);
pw.println("服务器返回:"+str);
}
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
发送端:
package p1;
import java.net.*;
import java.io.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class c1 {
public static void main(String[] args)
{
try {
InetAddress add=InetAddress.getByName("localhost");
Socket s=new Socket(add,8888);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String message=reader.readLine();
pw.println(message);
pw.flush();
//String str;
String str=br.readLine();
System.out.println(str);
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
----------------解决方案--------------------------------------------------------
像你这个程序只能接受一个连接,并且每个连接只能发送和接收一条消息,就断掉了
所以会抛出java.net.SocketException: Connection reset异常!!
它表示连接重置
----------------解决方案--------------------------------------------------------
那该如何解决呢?
----------------解决方案--------------------------------------------------------
起两个线程
一边一个,一直循环通讯下去
----------------解决方案--------------------------------------------------------
接收端,也就是服务器端
[CODE]/*
* Receive.java
*
* Created on 2006年12月19日, 下午2:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testTCP;
/**
*
* @author lbf
*/
import java.io.*;
import java.net.*;
public class Receive{
/** Creates a new instance of Receive */
public Receive() {
listen();
}
private void listen(){
try{
ServerSocket ss=new ServerSocket(10000);
while(true){
Socket s=ss.accept();
new HandleConnect(s).start();
}
}
catch(Exception exe){
exe.printStackTrace();
}
}
private class HandleConnect extends Thread{
private BufferedReader br;
public HandleConnect(Socket s){
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(Exception exe ){
exe.printStackTrace();
}
}
public void run(){
while(true){
try{
System.out.println("收到的信息:"+br.readLine());
}
catch(Exception exe ){
exe.printStackTrace();
break;
}
}
}
}
public static void main(String[] args) {
new Receive();
}
}[/CODE]
----------------解决方案--------------------------------------------------------
发送端
[CODE]/*
* Send.java
*
* Created on 2006年12月19日, 下午2:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testTCP;
/**
*
* @author lbf
*/
import java.io.*;
import java.net.*;
public class Send extends Thread{
private PrintWriter pw;
/** Creates a new instance of Send */
public Send(String name) {
super(name);
initSocket();
this.start();
}
private void initSocket(){
try{
Socket s=new Socket("localhost",10000);
pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
}
catch(Exception exe ){
exe.printStackTrace();
}
}
public void run(){
while(true){
try{
Thread.sleep(1000);
pw.println(this.getName()+"这是通过TCP发过去的信息!");
pw.flush();
}catch(Exception exe){
exe.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
new Send("线程1");
}
}[/CODE]
----------------解决方案--------------------------------------------------------
,谢谢!!
----------------解决方案--------------------------------------------------------