黑马程序员技术交流社区
标题:
用TCP实现聊天室功能,请完成程序的空余部分,期末试题
[打印本页]
作者:
黎健东
时间:
2012-7-21 20:31
标题:
用TCP实现聊天室功能,请完成程序的空余部分,期末试题
发一个我大三java期末考试的时候的上机试题,本题25分,全卷100分。
1.JAVA编程:用TCP实现聊天室功能,程序有两个文件Server.java和Client.java,请完成程序的空余部分。
//Server.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
class RecvMegSock extends Thread
{
LinkedList SocketList = null;
Socket ClientSock = null;
JTextArea jTextArea1;
RecvMegSock(LinkedList list,Socket s,JTextArea t)
{
SocketList = list;
jTextArea1 = t;
ClientSock = s;
start();
}
public void run()
{
while(true)
{
try
{
DataInputStream in = 代码1
String word = in.readUTF();
jTextArea1.setText(jTextArea1.getText() + word + "\n");
for(int i = 0;i < SocketList.size();i++)
{
Socket s = (Socket)SocketList.get(i);
DataOutputStream out = 代码2
out.writeUTF( 代码3 );
}
}
catch (IOException ex)
{
for(int i = 0;i < SocketList.size();i++)
{
Socket s = (Socket)SocketList.get(i);
if(s == ClientSock)
SocketList.remove(s);
}
for(int i = 0;i < SocketList.size();i++)
{
Socket s = (Socket)SocketList.get(i);
try
{
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeUTF(ClientSock.getInetAddress() + "已经退出!!\n");
}
catch (IOException ex1)
{ }
}
jTextArea1.setText(jTextArea1.getText() + ClientSock.getInetAddress() + "已经退出!!" + "\n");
return;
}
}
}
}
class AcceptSockThread extends 代码4
{
ServerSocket ServerSock = null;
LinkedList SocketList = null;
JTextArea TextArea;
AcceptSockThread(ServerSocket s,LinkedList list,JTextArea t)
{
ServerSock = s;
SocketList = list;
TextArea = t;
start();
}
public void run()
{
while(true)
{
Socket ClientSock = null;
try
{
boolean same = false;
ClientSock = ServerSock. 代码5
new RecvMegSock(SocketList,ClientSock,TextArea);
SocketList.add(ClientSock);
for(int i = 0;i < SocketList.size();i++)
{
Socket s = (Socket)SocketList.get(i);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeUTF(ClientSock.getInetAddress() + "成功连接服务器\n");
}
TextArea.setText(TextArea.getText() + ClientSock.getInetAddress() + "连接到服务器\n");
}
catch (IOException ex)
{ }
}
}
}
public class Server extends javax.swing.JDialog implements ActionListener
{
ServerSocket server = null;
LinkedList SocketList = null;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
public Server(java.awt.Frame parent, boolean modal)
{
super(parent, modal);
initComponents();
try
{
server = new ServerSocket(8888);
SocketList = new LinkedList();
new AcceptSockThread(server,SocketList,jTextArea1);
}
catch (IOException ex)
{ }
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(10, 10, 380, 250);
getContentPane().add(jTextField1);
jTextField1.setBounds(10, 270, 290, 21);
jButton1.setText("发送");
jButton1.addActionListener(this);
jTextField1.addActionListener(this);
getContentPane().add(jButton1);
jButton1.setBounds(310, 270, 80, 25);
pack();
}
public void actionPerformed(java.awt.event.ActionEvent evt) {
if(SocketList.size() == 0)
{
jTextArea1.setText(jTextArea1.getText() + "没有人连接!!" + "\n");
return;
}
for(int i = 0;i < SocketList.size();i++)
{
DataOutputStream out;
try
{
out = new DataOutputStream(((Socket) SocketList.get(i)).getOutputStream());
if(jTextField1.getText() != null)
out.writeUTF("服务器: "+jTextField1.getText());
}
catch (IOException ex)
{ }
}
jTextArea1.setText(jTextArea1.getText() + "服务器: "+ jTextField1.getText() + "\n");
}
public static void main(String args[])
{
Server dlg = new Server(null,true);
dlg.setBounds(300, 200, 400, 325);
dlg.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dlg.setVisible(true);
}
}
复制代码
作者:
黎健东
时间:
2012-7-21 20:31
//Client.java
import java.awt.event.*;
import java.io.*;
import java.net.*;
class RecvMegThread extends Thread
{
DataInputStream in;
DataOutputStream out;
Socket ServerSock;
javax.swing.JTextArea TextArea;
Client MainDialog;
RecvMegThread(Client dlg)
{
MainDialog = dlg;
ServerSock = dlg.ServerSock;
TextArea = dlg.jTextArea1;
try
{
in = new DataInputStream(ServerSock.getInputStream());
out = new DataOutputStream(ServerSock.getOutputStream());
}
catch (IOException ex)
{ }
start();
}
public void run()
{
while(true)
{
try
{
String word = in.readUTF();
TextArea.setText(TextArea.getText() + word + "\n");
}
catch (IOException ex)
{
TextArea.setText(TextArea.getText() + "和服务器失去了联系!!" + "\n");
MainDialog.ServerSock = null;
return;
}
}
}
}
public class Client extends javax.swing.JDialog implements ActionListener
{
private javax.swing.JTextField IP;
private javax.swing.JTextField Port;
private javax.swing.JTextField Username;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
Socket ServerSock;
String username;
public Client(java.awt.Frame parent, boolean modal)
{
super(parent, modal);
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
IP = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
Port = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
Username = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(10, 40, 570, 220);
getContentPane().add(jTextField1);
jTextField1.setBounds(10, 270, 460, 21);
jButton1.setText("发送");
jButton1.addActionListener(this);
getContentPane().add(jButton1);
jButton1.setBounds(495, 270, 80, 25);
jLabel1.setText("服务器IP:");
getContentPane().add(jLabel1);
jLabel1.setBounds(10, 10, 70, 20);
IP.setText("127.0.0.1");
getContentPane().add(IP);
IP.setBounds(70, 10, 100, 21);
jLabel2.setText("端口:");
getContentPane().add(jLabel2);
jLabel2.setBounds(180, 10, 50, 20);
Port.setText("8888");
getContentPane().add(Port);
Port.setBounds(220, 10, 70, 21);
jLabel3.setText("用户名:");
getContentPane().add(jLabel3);
jLabel3.setBounds(300, 10, 70, 20);
Username.setText("无名氏");
getContentPane().add(Username);
Username.setBounds(360, 10, 100, 21);
jButton2.setText("连接");
jButton2.addActionListener(this);
jTextField1.addActionListener(this);
getContentPane().add(jButton2);
jButton2.setBounds(490, 10, 90, 25);
pack();
}
public void actionPerformed(java.awt.event.ActionEvent evt) {
if ((evt.getSource()==jButton1)||(evt.getSource()==jTextField1))
{
if(ServerSock == null)
{
jTextArea1.setText(jTextArea1.getText() + "没有连接服务器!!" + "\n");
return;
}
try
{
DataOutputStream out = new DataOutputStream(ServerSock.getOutputStream());
if(jTextField1.getText() != null)
out.writeUTF(username + ": " + jTextField1.getText());
}
catch (IOException ex)
{ }
}
if( evt.getSource()==jButton2 )
{
if(IP.getText().equals("") || Port.getText().equals("") )
{
this.jTextArea1.setText(this.jTextArea1.getText() + "IP 和 端口不能为空!!\n");
return;
}
try
{
Integer.parseInt(Port.getText());
}
catch(Exception e)
{
this.jTextArea1.setText(this.jTextArea1.getText() + "端口号应为数字!!\n");
return;
}
try
{
if(ServerSock != null)
{
String str = "" + ServerSock.getInetAddress() + ServerSock.getPort();
System.out.println(str);
System.out.println("/" + IP.getText() + Port.getText());
if(str.equals("/" + IP.getText() + Port.getText()))
{
this.jTextArea1.setText(this.jTextArea1.getText() + "已经连接了服务器!!\n");
return;
}
}
ServerSock = new Socket(IP.getText(), Integer.parseInt(Port.getText()));
username = Username.getText();
new RecvMegThread(this);
}
catch (Exception ex)
{
jTextArea1.setText(jTextArea1.getText() + "连接不上服务器!!\n");
return;
}
}
}
public static void main(String args[])
{
Client dlg = new Client(null,true);
dlg.setBounds(300, 200, 605, 340);
dlg.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dlg.setVisible(true);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2