Socket套接字传输流_JAVA技术篇
Socket和ServerSocket类是java.net包中最常用的类。
实际上套接了只是计算机上已编号的端口。
Socket通信是一种基于TCP协议,建立稳定连接的点对点的通信。
这种通信方式实时,快速,安全性高,但是它很占用系统的资源。
服务器端代码如下:----------------------------------------------------------
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Simple水乡乌镇SocketServer {
private ServerSocket serverSocket;
int portNumber = 1777;
Socket水仙花的作文 socket;
String str ;
public SimpleSocketServer{
str = "Hello Client";
try {
serverSocket = new ServerSocket;
System.out.println;
System.out.println;
socket = serverSocket.accept;
ObjectOutputStream oos =
new ObjectOutputStream);
oos.writeObject;
oos.close;
socket.close;
} catch {
System.out.println;
}
}
public static void main {
SimpleSocketServer sss = new SimpleSocketServer;
}
}
一个简单的客户端代码如下:----------------------------------------------------
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SimpleSocketClient {
private S年级英语ocket socket;
int portNumber = 1777;
String str = "";
public SimpleSocketClient{
try {
socket = new Socket,portNumber);
ObjectInputStream ois =
new ObjectInputStream);
str = ois.readObject;
System.out.println;
校园生活作文400字 } catch {
System.out.println;
}
}
public static void main {
SimpleSocketClient ssp = new SimpleSocketClient;
}
}