本帖最后由 刘策 于 2013-3-30 13:13 编辑
package day13;
import java.io.*;
import java.net.*;
class Demo{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10086);
Socket s = ss.accept();
new Thread(new Rece(s)).start();
new Thread(new Test(s)).start();
s.close();
ss.close();
}
}
class Rece implements Runnable{
private Socket s;
Rece(Socket s){
this.s = s;
}
public void run(){
try{
System.out.println("haha");
String ip = s.getInetAddress().getHostAddress();
BufferedReader bufin =
new BufferedReader(new InputStreamReader( s.getInputStream()));
System.out.println(ip+"..........connected");
//byte[] buf = new byte[1024];
//int len = 0;
String data = null;
while((data=bufin.readLine()) !=null){
System.out.println(data);
}
}
catch(Exception e){
throw new RuntimeException("YYYYYNNNNNOOOO" );
}
}
}
class Test implements Runnable{
private Socket s;
Test(Socket s){
this.s = s;
}
public void run(){
try{
String ip = s.getInetAddress().getHostAddress();
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(s.getOutputStream());
String str = null;
while((str=bufr.readLine()) !=null)
{
System.out.println("我:"+str);
if("886".equals(str))
break;
pw.println(str);
System.out.println("数据已经发出");
}
bufr.close();
s.close();
}
catch(Exception e){
throw new RuntimeException("NoNONON" );
}
}
}
写的不怎么好,希望能帮忙找一下原因。谢谢呀!
|