客户端
服务器端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MultiJabberClient {
static final int MAX_THREADS = 10;
public void start() throws UnknownHostException, InterruptedException {
InetAddress addr = InetAddress.getByName("bupt"); // 主机名
while (true) {
if (JabberClientThread.getThreadCount() < MAX_THREADS)
new JabberClientThread(addr); // addr是服务器的地址
}
}
public static void main(String args[]) throws UnknownHostException,
InterruptedException {
new MultiJabberClient().start();
}
}
class JabberClientThread extends Thread {
private static int counter = 0;
private static int threadcount = 0;
private int id = counter++;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public JabberClientThread(InetAddress addr) {
System.out.println("Making Client " + id);
threadcount++;
try {
socket = new Socket(addr, MultiJabberServer.PORT);
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
start();
} catch (IOException e) {
e.printStackTrace();
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void run() {
try {
out.println("hello");
out.println("bye");
String str = in.readLine();
System.out.println(str);
// }
out.println("END");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Client " + id + " closing...");
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
threadcount--;
}
}
public static int getThreadCount() {
return threadcount;
}
}
服务器端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MultiJabberServer {
public static final int PORT = 8080; //端口可以任意指定大于1024的端口号
public void start() throws IOException{
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server started.");
try{
while(true){
final Socket socket = s.accept();//阻塞,直到建立一个连接,通常,阻塞是由循环产生的。
try{
new ServerOneJabber(socket); //监听到一个端口,就新建一个线程进行执行
}
catch(Exception e){
socket.close();
}
}
}
finally{
s.close();
}
}
public static void main(String args[]) throws IOException{
new MultiJabberServer().start();
}
}
class ServerOneJabber extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServerOneJabber(Socket s) throws IOException{
socket = s;
in = new BufferedReader(
new InputStreamReader(socket.getInputStream())); //负责读取来自客户端的请求
out = new PrintWriter(socket.getOutputStream(), true); //
用来向客户端会送响应的
start();
}
public void run() {
System.out.println(socket.getInetAddress()+":"+socket.getPort());
try{
while(true){
String str = in.readLine(); //从客户端读取的字符串
if(str.equals("END"))
break;
System.out.println("Echoing: " + str);
out.println(str);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally{
System.out.println("closing...");
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}