黑马程序员技术交流社区
标题:
两线程控制TCP服务端和客户端的问题,
[打印本页]
作者:
zhouxp3323
时间:
2012-3-31 16:00
标题:
两线程控制TCP服务端和客户端的问题,
class TCPClient2 implements Runnable{
@Override
public void run() {
try {
Socket s = new Socket("192.168.1.101",10004);
OutputStream out = s.getOutputStream();
out.write("服务端,你好".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
} catch (Exception e) {
}
}
}
class TCPServer2 implements Runnable{
@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"....connected");
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int len = is.read();
System.err.println(new String(buf,0,len));
OutputStream os = s.getOutputStream();
os.write("哥们收到,你好".getBytes());
s.close();
ss.close();
} catch (Exception e) {
}
}
}
public class TCPDemo2 {
public static void main(String[] args) {
new Thread(new TCPClient2()).start();
new Thread(new TCPServer2()).start();
}
}
这是我整个程序,运行结果是:
192.168.1.101....connected
[][][][][][][][][][][][][][][][][][]
哥们收到,你好
怎么客户端的信息没显示呢,谁能看明白吗?
作者:
zhouxp3323
时间:
2012-3-31 18:13
yocean 发表于 2012-3-31 16:21
呵,对,加了那个能运行出来了,不过打印出来的“服务端,你好”怎么是红色的呢?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2