public class InternetServerTest {
/*
tcp协议是一种以固定连线为基础的协议 保证数据的可靠传输
udp是无连接通信协议 不保证数据的传输
tcp程序基础
1.服务端创建一个serversocket调用accept方法等待客户端连接
2.客户端创建一个socket请求与服务端连接
3.服务端接受连接请求 同时创建一个新的socket等待其他的连接
InetAddress类与IP地址有关的信息
Socket套接字 ServerSocket
* */
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8000);
System.out.println("服务端套接字创建成功,等待连接");
Socket socket = server.accept();
OutputStream os = socket.getOutputStream();
os.write("服务端收到客户端消息".getBytes());
InputStream is = socket.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
int length = 0;
while ((length = bf.read()) != -1) {
System.out.println((char) length);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class InternetClientTest {
public static void main(String[] args) {
try {
System.out.println("客户端尝试获取连接");
Socket socket = new Socket("127.0.0.1", 8000);
OutputStream os = socket.getOutputStream();
os.write("我是client".getBytes());
socket.shutdownOutput();
InputStream is = socket.getInputStream();
int len=0;
byte[] bytes=new byte[1024];
while ((len=is.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
}catch (Exception e){
e.printStackTrace();
}
}
}