关键字: socket
java socket编程范例
Stream Socket
一 总述
Stream Socket与Datagram Socket不同,它有一个永久性的链接,可以确保数据可靠的发送到对方。但是Stream Socket所占的资源更多。
Java主要通过Socket及ServerSocket来实现Stream Socket,一个用于客户端,另一个用户服务器端。
二 Socket类
Socket类的几个重要方法如下:
(1) Socket(String host, int port)
(2) Socket(InetAddress address, int port)
(3) synchronized void close()
(4) InputStream getInputStream()
(5) OutputStream getOutputStream()
可以看出Stream Socket中的Socket与Datagram Socket中的Socket的区别:
(1) 在构造函数中,这里需要指定服务器的地址与端口,用于它发送数据之前建立链接,而Datagram的Socket不需要建立链接,它仅需要在要发送的数据包中包含地址信息即可;
(2) Stream Socket的两个重要方法和其它流的方法类似,而且在操作上也相似,其实在操作Stream Socket时,只要建立了链接,那么以后的操作和流的操作一样了,你可以忘了你在操作Socket,只当做自己在操作普通的流。
三 ServerSocket类
ServerSocket有以下几个重要的方法:
(1) ServerSocket(int port):监听指定端口,监听时间为默认值50;
(2) ServerSocket(int port, int count):监听指定端口,并指定监听时间;
(3) ServerSocket(int port, int backlog, InetAddress bindAddr):只接收指定接口发过来的数据;
(4) Socket accept():accpet方法会返回一个Socket对象,使用该Socket对象的getInputStream及getOutputStream方法获取输入及输出流。由此可见无论是服务器端还是客户端,它们都是通过Socket对象来收发数据的。程序执行到accpet时会进入监听状态,不再向下执行一直到接收到数据。
(5) void close()
四 实例
以下是关于Stream Socket的一个例子,在这个例子中使用单独的二个类Receiver和Sender负责对指定的输入及输出流进行操作,用另一个类SocketPerformer来创建Receiver和Sender这两个类的实例,并传入指定的输入及输出流,而在Client及Server这两个类中,负责创建SocketPerformer对象,并传入指定的Socket。因此这五个类在功能上分为三层,第一层负责对流进行操作,第二层负责创建指定的Socket流,并这些Socket流传到第一层,第三层负责创建Socket并将这些Socket传到第二层。
Sender类:
package socketChat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Sender extends Thread {
PrintWriter writer;
public Sender(PrintWriter theWriter) {
writer = theWriter;
}
public void run() {
try {
while (true) {
BufferedReader consoleReader = new BufferedReader(
new InputStreamReader(System.in));
String userInput = consoleReader.readLine();
writer.write(userInput + "/r/n");
writer.flush(); // send the data in buffer immediately
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
Receiver类:
package socketChat;
import java.io.BufferedReader;
public class Receiver extends Thread {
BufferedReader reader;
public Receiver(BufferedReader theSocketReader) {
reader = theSocketReader;
}
public void run() {
while (true) {
try {
String words = reader.readLine();
System.out.println("/r/nRecieved: " + words);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
SocketPerformer类:
package socketChat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketPerformer {
Socket socket;
public SocketPerformer(Socket theSoc){
socket = theSoc;
}
public void execut() throws Exception{
BufferedReader socketReader = new BufferedReader(
new InputStreamReader( socket.getInputStream()));
PrintWriter socketWriter = new PrintWriter(socket.getOutputStream());
System.out.println("connection built !");
Receiver rec = new Receiver(socketReader);
rec.start();
Sender sender = new Sender(socketWriter);
sender.start();
}
}
Client类:
package socketChat;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
String ip = "127.0.0.1";
int port = 30000;
System.out.println("connecting to server: " + ip
+ " at port: " + port + " ...");
Socket soc = new Socket(ip, port);
new SocketPerformer(soc).execut();
}
}
Server类:
package socketChat;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(30000, 5);
Socket socket = null;
System.out.println("waiting incoming connection...");
socket = server.accept();
System.out.println("a connection built!");
System.out.println("the peer ip is: " + socket.getInetAddress().getHostAddress());
new SocketPerformer(socket).execut();
server.close();
}
}
Stream Socket
一 总述
Stream Socket与Datagram Socket不同,它有一个永久性的链接,可以确保数据可靠的发送到对方。但是Stream Socket所占的资源更多。
Java主要通过Socket及ServerSocket来实现Stream Socket,一个用于客户端,另一个用户服务器端。
二 Socket类
Socket类的几个重要方法如下:
(1) Socket(String host, int port)
(2) Socket(InetAddress address, int port)
(3) synchronized void close()
(4) InputStream getInputStream()
(5) OutputStream getOutputStream()
可以看出Stream Socket中的Socket与Datagram Socket中的Socket的区别:
(1) 在构造函数中,这里需要指定服务器的地址与端口,用于它发送数据之前建立链接,而Datagram的Socket不需要建立链接,它仅需要在要发送的数据包中包含地址信息即可;
(2) Stream Socket的两个重要方法和其它流的方法类似,而且在操作上也相似,其实在操作Stream Socket时,只要建立了链接,那么以后的操作和流的操作一样了,你可以忘了你在操作Socket,只当做自己在操作普通的流。
三 ServerSocket类
ServerSocket有以下几个重要的方法:
(1) ServerSocket(int port):监听指定端口,监听时间为默认值50;
(2) ServerSocket(int port, int count):监听指定端口,并指定监听时间;
(3) ServerSocket(int port, int backlog, InetAddress bindAddr):只接收指定接口发过来的数据;
(4) Socket accept():accpet方法会返回一个Socket对象,使用该Socket对象的getInputStream及getOutputStream方法获取输入及输出流。由此可见无论是服务器端还是客户端,它们都是通过Socket对象来收发数据的。程序执行到accpet时会进入监听状态,不再向下执行一直到接收到数据。
(5) void close()
四 实例
以下是关于Stream Socket的一个例子,在这个例子中使用单独的二个类Receiver和Sender负责对指定的输入及输出流进行操作,用另一个类SocketPerformer来创建Receiver和Sender这两个类的实例,并传入指定的输入及输出流,而在Client及Server这两个类中,负责创建SocketPerformer对象,并传入指定的Socket。因此这五个类在功能上分为三层,第一层负责对流进行操作,第二层负责创建指定的Socket流,并这些Socket流传到第一层,第三层负责创建Socket并将这些Socket传到第二层。
Sender类:
package socketChat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Sender extends Thread {
PrintWriter writer;
public Sender(PrintWriter theWriter) {
writer = theWriter;
}
public void run() {
try {
while (true) {
BufferedReader consoleReader = new BufferedReader(
new InputStreamReader(System.in));
String userInput = consoleReader.readLine();
writer.write(userInput + "/r/n");
writer.flush(); // send the data in buffer immediately
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
Receiver类:
package socketChat;
import java.io.BufferedReader;
public class Receiver extends Thread {
BufferedReader reader;
public Receiver(BufferedReader theSocketReader) {
reader = theSocketReader;
}
public void run() {
while (true) {
try {
String words = reader.readLine();
System.out.println("/r/nRecieved: " + words);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
SocketPerformer类:
package socketChat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketPerformer {
Socket socket;
public SocketPerformer(Socket theSoc){
socket = theSoc;
}
public void execut() throws Exception{
BufferedReader socketReader = new BufferedReader(
new InputStreamReader( socket.getInputStream()));
PrintWriter socketWriter = new PrintWriter(socket.getOutputStream());
System.out.println("connection built !");
Receiver rec = new Receiver(socketReader);
rec.start();
Sender sender = new Sender(socketWriter);
sender.start();
}
}
Client类:
package socketChat;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
String ip = "127.0.0.1";
int port = 30000;
System.out.println("connecting to server: " + ip
+ " at port: " + port + " ...");
Socket soc = new Socket(ip, port);
new SocketPerformer(soc).execut();
}
}
Server类:
package socketChat;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(30000, 5);
Socket socket = null;
System.out.println("waiting incoming connection...");
socket = server.accept();
System.out.println("a connection built!");
System.out.println("the peer ip is: " + socket.getInetAddress().getHostAddress());
new SocketPerformer(socket).execut();
server.close();
}
}