标签:
在两台机器上测试的要关闭两台机器的防火墙。。。
贴:
ChatServer
import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChatServer extends JFrame implements ActionListener { JFrame frame = null; static JTextArea info = null; JTextField input = null; JButton btn = null; JPanel panel = null; static ServerSocket serversocket = null; static Socket connectToClient = null; static DataInputStream inFromClient = null; static DataOutputStream outToClient = null; ChatServer() { super("ChatServer"); info = new JTextArea(); input = new JTextField(15); btn = new JButton("Send"); panel = new JPanel(); btn.addActionListener(this); panel.add(input); panel.add(btn); add(info,BorderLayout.CENTER); add(panel,BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); setBounds(300,300,600,300); } public void actionPerformed(ActionEvent ae) { String outStr; try { outStr = input.getText().toString(); outToClient.writeUTF(outStr); outToClient.flush(); input.setText(""); info.append("me: " + outStr + ‘\n‘); }catch(IOException e) { e.printStackTrace(); } } public static void main(String args[]) { String inStr; new ChatServer(); try { InetAddress inet = InetAddress.getLocalHost(); info.append(inet.getHostAddress() + ‘\n‘); info.append(InetAddress.getLocalHost().toString() + ‘\n‘); serversocket = new ServerSocket(5500); connectToClient = serversocket.accept(); inFromClient = new DataInputStream(connectToClient.getInputStream()); outToClient = new DataOutputStream(connectToClient.getOutputStream()); while(true) { inStr = inFromClient.readUTF(); info.append("opposite: " + inStr + ‘\n‘); } }catch(IOException e) { e.printStackTrace(); } } }
ChatClient
客户端的代码需要服务器端的ip地址。
import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class ChatClient extends JFrame implements ActionListener { JButton btn = null; JPanel panel = null; static JTextArea info = null; JTextField input = null; static String host = "202.199.116.102"; static Socket connectToServer = null; static DataInputStream inFromServer = null; static DataOutputStream outToServer = null; ChatClient() { JFrame client = new JFrame("ChatClient"); btn = new JButton("Send"); panel = new JPanel(); info = new JTextArea(); input = new JTextField(15); panel.add(input); panel.add(btn); client.add(info,BorderLayout.CENTER); client.add(panel,BorderLayout.SOUTH); btn.addActionListener(this); client.setDefaultCloseOperation(EXIT_ON_CLOSE); client.setVisible(true); client.setBounds(300,300,600,300); } public void actionPerformed(ActionEvent ae) { String outStr; try { outStr = input.getText().toString(); outToServer.writeUTF(outStr); outToServer.flush(); info.append("me: " + outStr + ‘\n‘); }catch(IOException e) { e.printStackTrace(); } input.setText(""); } public static void main(String args[]) { String inStr; new ChatClient(); try { connectToServer = new Socket(host,1080); inFromServer = new DataInputStream(connectToServer.getInputStream()); outToServer = new DataOutputStream(connectToServer.getOutputStream()); while(true) { inStr = inFromServer.readUTF(); info.append("opposite: " + inStr + ‘\n‘); } }catch(IOException e) { e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/burning-flame/p/5405495.html