客户端与服务器通信

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

SimpleChatServer.java

package test.chatclient;

import java.io.*;

import .*;

import java.util.*;

public class SimpleChatServer {

ArrayList clientOutputStreams;

public static void main(String[] args){

new SimpleChatServer().go();

}

public class ClientHandler implements Runnable{

BufferedReader reader;

Socket sock;

public ClientHandler(Socket clientSocket){

try{

sock = clientSocket;

InputStreamReader isReader = new InputStreamReader(sock.getInputStream());

reader = new BufferedReader(isReader);

}catch(Exception ex){

ex.printStackTrace();

}

}

@Override

public void run() {

String message;

try{

while((message = reader.readLine()) != null){

System.out.println("read " + message);

tellEveryone(message);

}

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public void tellEveryone(String message){

Iterator it = clientOutputStreams.iterator();

while(it.hasNext()){

try{

PrintWriter writer = (PrintWriter)it.next();

writer.println(message);

writer.flush();

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public void go(){

clientOutputStreams = new ArrayList();

try{

ServerSocket serverSock = new ServerSocket(5000);

while(true){

Socket clientSocket = serverSock.accept();

PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());

clientOutputStreams.add(writer);

Thread t = new Thread(new ClientHandler(clientSocket));

t.start();

System.out.println("got a connection");

}

}catch(Exception ex){

ex.printStackTrace();

}

}

}

SimpleChatClient.java

package test.chatclient;

import java.io.*;

import .*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.JOptionPane;

public class SimpleChatClient {

JTextArea incoming;

JTextField outgoing;

BufferedReader reader;

PrintWriter writer;

Socket sock;

static String clientName;

public static void main(String[] args) {

SimpleChatClient client = new SimpleChatClient();

clientName = JOptionPane.showInputDialog("Please input the client name :");

client.go();

}

public void go() {

// build GUI

JFrame frame = new JFrame(clientName + "'s Chat Client");

JPanel mainPanel = new JPanel();

incoming = new JTextArea(15,20);

incoming.setLineWrap(true);

incoming.setWrapStyleWord(true);

incoming.setEditable(false);

JScrollPane qScroller = new JScrollPane(incoming);

qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ ALW AYS);

qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLL BAR_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);

} // close go

相关文档
最新文档