客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.137.1",10086);
OutputStream os = s.getOutputStream();
os.write("你好我来了".getBytes());
s.close();
}
}
服务器
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10086);
Socket s = ss.accept();
byte[] bytes = new byte[1024];
InputStream is = s.getInputStream();
int len = is.read(bytes);
System.out.println(new String(bytes,0,len));
ss.close();
}
}