最近比较懒,好久没有更新博客了。这个代码早就写了,但是一直没有发出来,今天又更新了一下功能,发出来。算是我最近重新复习线程的一个总结吧。以前就看过java的多线程处理,但是一直有点迷迷糊糊的,不是很清楚,这次通过写这个程序,算是有了点头绪了。而且以前没有用过socket这个对象,也是初次接触,不错的东东,蛮有意思的。争取以后继续深化一下这方面。
程序分2部分,服务器端和客户端。服务器端负责信息的处理,如发送欢迎信息,帮助信息之类的,统一分发消息等。客户端呢,则是简单的接受来自服务端的消息,以及控制台的信息输入。
服务器端:
import
java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
/** */ /**
* Lan Communication Server
*
* @author webkkk
* @version 1.0
*/
public class TestSocket ... {
private final int defalutPort = 6665;
TestSocketServerCenter serverCenter;
public TestSocket() ...{
try ...{
ServerSocket server = new ServerSocket(defalutPort);
serverCenter = new TestSocketServerCenter(server, this);
serverCenter.start();
TestSocketInfoCenter infoCenter = new TestSocketInfoCenter(this);
infoCenter.start();
} catch (IOException e) ...{
printMsg(e.getMessage());
}
}
/** *//**
* @param args
*/
public static void main(String[] args) ...{
new TestSocket();
}
public void printMsg(String strInfo) ...{
System.err.println(String.valueOf(new Date()) + "->" + strInfo);
}
}
class TestSocketServerCenter extends Thread ... {
ServerSocket server;
TestSocket main;
Vector clientManager = new Vector();
Vector infoManager = new Vector();
int intClientCount = 0;
int intActivedSeq = 0;
public TestSocketServerCenter(ServerSocket server, TestSocket main) ...{
this.server = server;
this.main = main;
}
public void run() ...{
while (true) ...{
try ...{
Socket socket = server.accept();
TestSocketServer server = new TestSocketServer(socket, this, intClientCount);
server.start();
clientManager.add(server);
infoManager.add("User-" + intClientCount);
welcome(socket);
intClientCount++;
} catch (IOException e) ...{
main.printMsg("[TestSocketServerCenter]" + e.getMessage());
}
}
}
private void welcome(Socket socket) ...{
intActivedSeq = intClientCount;
allMessage("joined in");
StringBuffer sb = new StringBuffer("------------------------------ ");
sb.append("Welcome " + infoManager.get(intActivedSeq) + " ");
sb.append("ps:You can use help command to learn about command. ");
sb.append("------------------------------");
singleMessage(socket, sb.toString());
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
/** */ /**
* Lan Communication Server
*
* @author webkkk
* @version 1.0
*/
public class TestSocket ... {
private final int defalutPort = 6665;
TestSocketServerCenter serverCenter;
public TestSocket() ...{
try ...{
ServerSocket server = new ServerSocket(defalutPort);
serverCenter = new TestSocketServerCenter(server, this);
serverCenter.start();
TestSocketInfoCenter infoCenter = new TestSocketInfoCenter(this);
infoCenter.start();
} catch (IOException e) ...{
printMsg(e.getMessage());
}
}
/** *//**
* @param args
*/
public static void main(String[] args) ...{
new TestSocket();
}
public void printMsg(String strInfo) ...{
System.err.println(String.valueOf(new Date()) + "->" + strInfo);
}
}
class TestSocketServerCenter extends Thread ... {
ServerSocket server;
TestSocket main;
Vector clientManager = new Vector();
Vector infoManager = new Vector();
int intClientCount = 0;
int intActivedSeq = 0;
public TestSocketServerCenter(ServerSocket server, TestSocket main) ...{
this.server = server;
this.main = main;
}
public void run() ...{
while (true) ...{
try ...{
Socket socket = server.accept();
TestSocketServer server = new TestSocketServer(socket, this, intClientCount);
server.start();
clientManager.add(server);
infoManager.add("User-" + intClientCount);
welcome(socket);
intClientCount++;
} catch (IOException e) ...{
main.printMsg("[TestSocketServerCenter]" + e.getMessage());
}
}
}
private void welcome(Socket socket) ...{
intActivedSeq = intClientCount;
allMessage("joined in");
StringBuffer sb = new StringBuffer("------------------------------ ");
sb.append("Welcome " + infoManager.get(intActivedSeq) + " ");
sb.append("ps:You can use help command to learn about command. ");
sb.append("------------------------------");
singleMessage(socket, sb.toString());
}