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就可以列出在線人員表
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25686.html