日期:2014-05-20  浏览次数:20900 次

菜鸟求帮忙(和 I/O 线程 有关 )
本帖最后由 kankanpian 于 2012-12-02 01:45:35 编辑 代码如下:

A:


import java.util.LinkedList;

public class ServerMain
{ public int duanKou;

public String zhuanTai;

    public LinkedList<String> userList;


public static void main(String[] args)
{
ServerMain svm = new ServerMain();

Thread th1 = new InPutThread(svm);

th1.start();




}
}



a:



import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class InPutThread extends Thread
{
private ServerMain serverMain;
private ServerSocket serverSocket;
private Socket socket;

public InPutThread(ServerMain serverMain)
{
this.serverMain = serverMain;
try
{
this.serverSocket = new ServerSocket(5000);
this.socket = serverSocket.accept();
}
catch (IOException e)
{

e.printStackTrace();
}



}

private void getUserName() throws Exception
{



InputStream ipt =  this.socket.getInputStream();

BufferedInputStream bfip = new BufferedInputStream(ipt);

             byte[] bt = new byte[1000];                          


int length = 0;

while (-1 != (length = bfip.read(bt, 0, 1000)))  
//先运行ServerMain 再运行ClientMain  程序运行到上面这行发生错误了?
/**     错误如下:
 *      java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at Sever.InPutThread.getUserName(InPutThread.java:51)
at Sever.InPutThread.run(InPutThread.java:69)

 * 
 * 
 */
{
String str = new String(bt, 0, length);
System.out.println(str);
}




}

@Override
public void run()
{
try
{
this.getUserName();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


B:

public class ClientMain
{
private String userName = "user1";

public String getUserName()
{
return userName;
}

public static void main(String[] args)
{
ClientMain clm = new ClientMain();

new OutPutThread(clm).start();
}
}



b:


import java.io.BufferedOutputStream;