public class TCPServer {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ServerSocket socket=new ServerSocket(6000);
Socket d= socket.accept();
//输入流
InputStream in=d.getInputStream();
byte[] b=new byte[1024];
int length=0;
while(-1!=(length=in.read(b, 0, b.length))){
String s=new String(b,0,length);
System.out.println(s);
}
//输出流
OutputStream ou=d.getOutputStream();
ou.write("哈哈".getBytes());
in.close();
ou.close();
}
}
package com.itheima.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class UDPClient {
public static void main(String[] args) throws UnknownHostException, Exception {
Socket so =new Socket("localHost",6000);
//输出流
OutputStream out=so.getOutputStream();
out.write("喂:你好?".getBytes());
//输入流
InputStream in=so.getInputStream();
byte[] b=new byte[1024];
int length=0;
while(-1!=(length=in.read(b, 0, b.length))){
String say=new String(b,0,length);
System.out.println(say);
}
out.close();
in.close();
}
}
控制台 只有 “喂 你好“,没有回复的 “哈哈” 怎么回事? |
|