基於tcp/ip協議,使用socket進行簡單的多使用者聊天室
阿新 • • 發佈:2019-01-27
heardfirst裡面有基於tcp/ip協議的socket聊天程式,這裡做一下記錄
SimpleChatClient客戶端類
public class SimpleChatClientA {
JTextArea incoimg;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
//建立了一個視窗介面
public void go(){
JFrame frame = new JFrame("Ludicrously Simple Chat Client" );
JPanel mainPanel = new JPanel();
incoimg = new JTextArea(15, 50);
incoimg.setLineWrap(true);
incoimg.setWrapStyleWord(true);
incoimg.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoimg);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400, 500);
frame.setVisible(true);
}
//初始化
private void setUpNetworking(){
try {
sock = new Socket("127.0.0.1", 5000);
writer = new PrintWriter(sock.getOutputStream());
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println("networking established");
} catch (IOException e) {
e.printStackTrace();
}
}
//監聽button
public class SendButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ev) {
writer.println("B"+outgoing.getText());
writer.flush();
outgoing.setText("");
outgoing.requestFocus();
}
}
public class IncomingReader implements Runnable{
@Override
public void run() {
String message;
try {
while((message=reader.readLine())!=null){
System.out.println("read:"+message);
incoimg.append(message+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new SimpleChatClientA().go();
}
}
伺服器端
public class VerySimpleChatServer {
private volatile ArrayList clientOutputStreams;
public class ClientHandler implements Runnable{
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSocket){
try {
sock = clientSocket;
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
String message;
try {
while((message=reader.readLine())!=null){
System.out.println("reader:"+message);
tellEveryone(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void go(){
clientOutputStreams = new ArrayList();
try {
synchronized(clientOutputStreams){
ServerSocket serverSock = new ServerSocket(5000);
while(true){
Socket clientSocket = serverSock.accept();
PrintWriter write = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(write);
Thread t= new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void tellEveryone(String message){
Iterator it = clientOutputStreams.iterator();
while(it.hasNext()){
PrintWriter write = (PrintWriter) it.next();
write.println(message);
write.flush();
}
}
public static void main(String[] args) {
new VerySimpleChatServer().go();
}
}