ServerSocket 简单例题:
package com.kaige123.net;
/*
*凯哥
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
public static void main(String[] args) throws Exception {
// 如果都被占用 那就会抛异常
// 建立好一个服务端 监听8080端口
ServerSocket server = new ServerSocket(8080);
// 等待客户端来连接服务器
Socket socket = server.accept(); //代码运行到这句就会卡主 堵塞 等待
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
byte[] b = new byte[1024 * 5];
int len = input.read(b);
String s = new String(b, 0, len);
System.out.println(s);
fin.read(b)
s = "hello 你好客户端!!";
output.write(s.getBytes());
output.close();
input.close();
socket.close();
}
}