Java Socket线程我们经常会用到的技术但是有很多程序员还是有不少的使用问题下面我们就看看如何才能进行有关的代码编写希望大家又说收获网络的伟大之一也是信息共享Server可以主动向所有Client广播消息同时Client也可以向其它Client发布消息
下面看看如何开发一个可以实时传递消息的程序
Java Socket线程的设计原理
服务器端接受客户端的连接请求同时启动一个线程处理这个连接线程不停的读取客户端输入然后把输入加入队列中等候处理在线程启动的同时将线程加入队列中以便在需要的时候定位和取出
{源码}
import javaio*;
import *;
import javautil*;
import javalang*;
public class Server extends ServerSocket
{
private static ArrayList User_List = new ArrayList();
private static ArrayList Threader = new ArrayList();
private static LinkedList Message_Array = new LinkedList();
private static int Thread_Counter = ;
private static boolean isClear = true;
protected static final int SERVER_PORT = ;
protected FileOutputStream LOG_FILE = new FileOutputStream(d:/connectlog true);
public Server() throws FileNotFoundException IOException
{
super(SERVER_PORT);
new Broadcast();
//append connection log
Calendar now = CalendargetInstance();
String str = [ + nowgetTime()toString() + ] Accepted a connection;
byte[] tmp = strgetBytes();
LOG_FILEwrite(tmp);
try
{
while (true)
{
Socket socket = accept();
new CreateServerThread(socket);
}
}
finally
{
close();
}
}
public static void main(String[] args) throws IOException
{
new Server();
}
// Broadcast
class Broadcast extends Thread
{
public Broadcast()
{
start();
}
public void run()
{
while (true)
{
if (!isClear)
{
String tmp = (String)Message_ArraygetFirst();
for (int i = ; i < Threadersize(); i++)
{
CreateServerThread client = (CreateServerThread)Threaderget(i);
clientsendMessage(tmp);
}
Message_ArrayremoveFirst();
isClear = Message_Arraysize() > ? false : true;
}
}
}
}
// CreateServerThread
class CreateServerThread extends Thread
{
private Socket client;
private BufferedReader in;
private PrintWriter out;
private String Username;
public CreateServerThread(Socket s) throws IOException
{
client = s;
in = new BufferedReader(new InputStreamReader(clientgetInputStream()));
out = new PrintWriter(clientgetOutputStream() true);
outprintln( Welcome to this chatroom );
outprintln(Input your nickname:);
start();
}
public void sendMessage(String msg)
{
outprintln(msg);
}
public void run()
{
try
{
int flag = ;
Thread_Counter++;
String line = inreadLine();
while (!lineequals(bye))
{
if (lineequals(l))
{
outprintln(listOnlineUsers());
line = inreadLine();
ntinue;
}
if (flag++ == )
{
Username = line;
User_Listadd(Username);
outprintln(listOnlineUsers());
Threaderadd(this);
pushMessage([< + Username + come on in >]);
}
else
{
pushMessage(< + Username + > + line);
}
line = inreadLine();
}
outprintln( See you bye! );
clientclose();
}
catch (IOException e)
{}
finally
{
try
{
clientclose();
}
catch (IOException e)
{}
Thread_Counter;
Threaderremove(this);
User_Listremove(Username);
pushMessage([< + Username + left>]);
}
}
private String listOnlineUsers()
{
String s =+ Online list +;
for (int i = ; i < User_Listsize(); i++)
{
s += [ + User_Listget(i) + ];
}
s += ++;
return s;
}
private void pushMessage(String msg)
{
Message_ArrayaddLast(msg);
isClear = false;
}
}
}
以上就是对Java Socket线程的详细介绍这就是程序运行后多用户登陆并且输入信息后的屏幕实现了信息的实时广播用户输入l就可以列出在线人员表