1.
Socket chatSocket = new Socket(“196.164.1.103”, 5000);
2.
1024~65535
3.
Socket chatSocket = new Socket(“127.0.0.1”, 5000);
InputStreamReader stream = new InputStreamReader(chatSocket.getInputStream());
BufferedReader reader = new BufferedReader(stream);
String message = reader.readLine();
4.
Socket chatSocket = new Socket(“127.0.0.1”, 5000);
PrintWriter writer = new PrintWriter(chatSocket.getOutputStream());
writer.println(“message to send”);
writer.print(“another message”);
5.
BufferedReader
InputStreamReader
Socket
6.
PrintWriter
Socket
7.
import java.net.*;
8.
reader.close();
9.
ServerSocket serverSock = new ServerSocket(4242); // 服务器
Socket sock = new Socket(“190.156.1.103”, 4242); // 客户端
Socket sock = serverSock.accept(); // 服务器
10.
try
{
ServerSocket serverSock = new ServerSocket(4242);
while(true)
{
Socket sock = serverSock.accept();
PrintWriter writer = new PrintWriter(sock.getOutputStream);
writer.println(“message to write”);
writer.close();
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
11.
try
{
Socket s = new Socket("127.0.0.1", 4242);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
reader.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}