import java.net.*;
import java.io.*;
public class Wangluo7
{
private ObjectOutputStream oos;
private ObjectInputStream ois;
private ServerSocket toserver;
private Socket server;
private int count=1;
public static void main(String[] args)
{
Wangluo7 wl=new Wangluo7();
wl.connectServer();
}

public void connectServer()
{
try
{
toserver=new ServerSocket(2007);
while(true)
{
waiting();
getStream();
processConnection();
closeConnection();
++count;
}
}catch(IOException e)
{
System.out.println("error"+e);
}catch(Exception ee)
{
System.out.println("error"+ee);
}
}

private void waiting() throws IOException
{
System.out.println("等待连接~~~~~~");
Socket connection=toserver.accept();
System.out.println("连接"+count+"次,从"+connection.getInetAddress().getHostName());
}

private void getStream() throws IOException
{
oos=new ObjectOutputStream(server.getOutputStream());
oos.flush();
ois=new ObjectInputStream(server.getInputStream());
System.out.println("获得IO流");
}

private void processConnection() throws IOException
{
String message="服务器端连接成功";
oos.writeObject(message);
oos.flush();
do
{
try
{
message=(String)ois.readObject();
System.out.println(message);
}catch(Exception eee)
{
System.out.println(eee);
}
}while(true);
}

private void closeConnection() throws IOException
{
System.out.println("关闭连接");
oos.close();
ois.close();
server.close();
}
}import java.net.*;
import java.io.*;
public class Wangluo8
{
private ObjectOutputStream oos;
private ObjectInputStream ios;
private String message="";
private String chatServer;
private Socket toclient;
public Wangluo8(String s)
{
chatServer=s;
}

public static void main(String args[])
{
Wangluo8 wl=new Wangluo8("127.0.0.1");
wl.connectClient();
}

public void connectClient()
{
try
{
connect();
getStream();
processConnection();
closeConnection();

}catch(ConnectException e)
{
System.out.println("error"+e);
}catch(IOException ee)
{
System.out.println("error"+ee);
}catch(Exception eee)
{
System.out.println("error"+eee);
}
}

private void connect() throws IOException
{
System.out.println("连接中~~~~~~");
toclient=new Socket(InetAddress.getByName(chatServer),2007);
System.out.println("连接"+toclient.getInetAddress().getHostName());
}

private void getStream() throws IOException
{
oos=new ObjectOutputStream(toclient.getOutputStream());
oos.flush();
ios=new ObjectInputStream(toclient.getInputStream());
System.out.println("获得IO流");
}

private void processConnection() throws IOException
{
do
{
try
{
message=(String)ios.readObject();
System.out.println(message);
}catch(Exception eee)
{
System.out.println(eee);
}
}while(true);
}

private void closeConnection() throws IOException
{
System.out.println("关闭连接");
oos.close();
ios.close();
toclient.close();
}
}
java Wangluo7
等待连接~~~~~~
连接1次,从127.0.0.1
errorjava.lang.NullPointerExceptionjava Wangluo8 1234sd
连接中~~~~~~
连接127.0.0.1
errorjava.net.SocketException: Connection reset

解决方案 »

  1.   


        private void waiting() throws IOException
        {
            System.out.println("等待连接~~~~~~");
            Socket connection=toserver.accept();//这个语句有逻辑问题。
            System.out.println("连接"+count+"次,从"+connection.getInetAddress().getHostName());
        }尝试修改为:
        private void waiting() throws IOException
        {
            System.out.println("等待连接~~~~~~");
            server=toserver.accept();//这个语句有逻辑问题。
            System.out.println("连接"+count+"次,从"+server.getInetAddress().getHostName());
        }
      

  2.   

    errorjava.lang.NullPointerExceptionerrorjava.net.SocketException: Connection reset从两个异常:一个为空指针异常,另一个为Socket异常(连接重置)中可以看出楼主写的代码,变量的声明与使用有问题,private Socket server;在类开始的时候就已经声明,然后又在方法中多做了一个Socket connection变量;所以出错,试修改成1楼的那样试试;