socket_tutorial.zip 本例包含两个类:JabberServer
是服务端,在9089端口监听来自客户端的请求。
JabberClient是客户端,它会首先和服务端建立连接。当客户端与服务器端的两个socket建立了专线连接后,连接的一端能向另一端连续写入字节,也能从另一端连续读入字节,也就是建立了专线连接的两个socket是以io流的方式进行数据交换的,java提供了Socket.getInputStream方法返回socket的输入流对象,Socket.getOuputStream方法返回socket的输出流对象。
只要连接的一端向该输出流对象写入了数据,连接的另一端就能从其输入流对象中读取到这些数据。
直接上代码。
- import java.io.BufferedReader;
-
import java.io.BufferedWriter;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
import java.io.OutputStreamWriter;
-
import java.io.PrintWriter;
-
import java.net.ServerSocket;
-
import java.net.Socket;
-
-
public class JabberServer {
-
// Choose a port outside of the range 1-1024:
-
public static final int PORT = 9089;
-
-
public static void main(String[] args) throws IOException {
-
ServerSocket s = new ServerSocket(PORT);
-
System.out.println("ServerSocket Started: " + s);
-
try {
-
// Blocks until a connection occurs:
-
Socket socket = s.accept();
-
try {
-
System.out
-
.println("ServerSocket Accect a socket, connection accepted: "
-
+ socket);
-
BufferedReader in = new BufferedReader(new InputStreamReader(
-
socket.getInputStream()));
-
// Output is automatically flushed
-
// by PrintWriter:
-
PrintWriter out = new PrintWriter(new BufferedWriter(
-
new OutputStreamWriter(socket.getOutputStream())), true);
-
while (true) {
-
// the server inputstream object read a line from socket
-
String str = in.readLine();
-
if (str.equals("END"))
-
break;
-
System.out.println("Echoing: " + str);
-
out.println(str);
-
}
-
// Always close the two sockets...
-
} finally {
-
System.out.println(" server closing...");
-
socket.close();
-
}
-
} finally {
-
s.close();
-
}
-
}
-
}
- import java.io.BufferedReader;
-
import java.io.BufferedWriter;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
import java.io.OutputStreamWriter;
-
import java.io.PrintWriter;
-
import java.net.InetAddress;
-
import java.net.Socket;
-
-
public class JabberClient {
-
-
public static void main(String[] args) throws IOException {
-
// Passing null to getByName() produces the
-
// special "Local Loopback" IP address, for
-
// testing on one machine w/o a network:
-
// InetAddress addr = InetAddress.getByName(null);
-
// Alternatively, you can use
-
// the address or name:
-
// InetAddress addr =
-
// InetAddress.getByName("127.0.0.1");
-
InetAddress addr = InetAddress.getByName("localhost");
-
System.out.println("address = " + addr);
-
Socket socket = new Socket(addr, 9089);
-
// Guard everything in a try-finally to make
-
// sure that the socket is closed:
-
try {
-
System.out.println("Client socket = " + socket);
-
BufferedReader in = new BufferedReader(new InputStreamReader(
-
socket.getInputStream()));
-
// Output is automatically flushed
-
// by PrintWriter:
-
PrintWriter out = new PrintWriter(new BufferedWriter(
-
new OutputStreamWriter(socket.getOutputStream())), true);
-
out.print("we are data come from client output, server can get us by its input stream");
-
for (int i = 0; i < 10; i++) {
-
out.println("howdy " + i);
-
String str = in.readLine();
-
System.out.println(str);
-
}
-
out.println("END");
-
} finally {
-
System.out.println("closing...");
-
socket.close();
-
}
-
}
-
-
}
阅读(839) | 评论(0) | 转发(0) |