学基础,写了个聊天室,基于多线程的,新手共勉之

wh070502219 2010-08-17 01:39:09
服务器端代码:(使用的TCP协议)


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MultiThreadServerChat extends JFrame{
private JTextField jtf=new JTextField();
private JTextArea jta=new JTextArea();
private ObjectOutputStream output;
private ObjectInputStream input;
private String string1="服务器:";
private String string2="客服端";
int clientNO=1;
static final int maxThreadLimit=10;//许可的最大线程数

public MultiThreadServerChat(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jtf,BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

jtf.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String serverString=null;
serverString=jtf.getText();
jtf.setText(null);
try {
output.writeObject(serverString);
output.flush();
jta.append(string1+serverString+"\n");

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});

setTitle("服务端");
setSize(400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try{
ServerSocket serverSocket=new ServerSocket(8000);

jta.append("服务器启动时间为:"+new Date()+"\n");




ThreadGroup g=new ThreadGroup("server chat");
while(true){
if(g.activeCount()>=maxThreadLimit){
Thread.sleep(100);
}
else{

Socket socket=serverSocket.accept();

output=new ObjectOutputStream(socket.getOutputStream());
//input=new ObjectInputStream(socket.getInputStream());


jta.append("Starting thread for client "+clientNO+"at "+new Date()+"\n");

Thread thread=new Thread(g, new ServerThread(socket));
thread.start();

clientNO++;
}
}
}
catch (Exception e) {
// TODO: handle exception
}
}

class ServerThread extends Thread{
private Socket socket;


public ServerThread(Socket socket) throws IOException{
this.socket=socket;
}

public void run(){
try {

ObjectInputStream input=new ObjectInputStream(socket.getInputStream());
//ObjectOutputStream output=new ObjectOutputStream(socket.getOutputStream());

while(true){
jta.append(string2+(clientNO-1)+" :"+(String)input.readObject()+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}


public static void main(String[] args){
new MultiThreadServerChat();
}

}

...全文
319 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fenseyouyu 2010-08-20
  • 打赏
  • 举报
回复
对了 需要的话 fenseyouyu@sohu.com 向我索要,大家一起探讨
fenseyouyu 2010-08-20
  • 打赏
  • 举报
回复
记得我的面试作品 就是c/s模式的聊天室。我花了两天时间写的。当时面试官说:你多久能做好?
我:两天。面试官:不用急,我们会耐心等你,2天不够我可以给你一个礼拜时间。
我两天就做好了,拿给他。结果通过了。嘿嘿。
其中包括,在线列表,聊天记录保存,公聊等。没有私聊哦。
oblyhua 2010-08-19
  • 打赏
  • 举报
回复

mklnjhyu 2010-08-19
  • 打赏
  • 举报
回复
您这个
怎么实现在局域网间
不通客户端之间的通信呀/
wh070502219 2010-08-17
  • 打赏
  • 举报
回复
客服端代码:


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ClientUseUDP extends JFrame{
private JTextField jtf=new JTextField();
private JTextArea jta=new JTextArea();
private String string1="服务器:";
private String string2="客服端:";

private byte[] buf=new byte[256];
private InetAddress address;
private DatagramSocket socket;
private DatagramPacket sendPacket;
private DatagramPacket receivePacket;

public ClientUseUDP(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jtf,BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

jtf.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {
jta.append(string2+jtf.getText().trim()+"\n");
sendPacket.setData(jtf.getText().getBytes());
socket.send(sendPacket);
jtf.setText(null);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});
setTitle("客户端");
setSize(400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try{
socket=new DatagramSocket();
address=InetAddress.getByName("localhost");
sendPacket=new DatagramPacket(buf, buf.length, address, 8000);
receivePacket=new DatagramPacket(buf, buf.length);

while(true){
Arrays.fill(buf,(byte)0);

socket.receive(receivePacket);
jta.append("服务器:"+new String(buf).trim()+"\n");

}

}
catch (Exception e) {
// TODO: handle exception
}
}

public static void main(String[] args){
new ClientUseUDP();
}

}
wh070502219 2010-08-17
  • 打赏
  • 举报
回复
服务器端代码:(UDP协议)


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;

import javax.sound.sampled.ReverbType;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ServerUseUDP extends JFrame{
private JTextField jtf=new JTextField();
private JTextArea jta=new JTextArea();
private String string1="服务器:";
private String string2="客服端:";

private byte[] buf=new byte[256];
private DatagramSocket socket;
private DatagramPacket sendPacket;
private DatagramPacket receivePacket;

public ServerUseUDP(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jtf,BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

jtf.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {
jta.append(string1+jtf.getText().trim()+"\n");
sendPacket.setData(jtf.getText().getBytes());
socket.send(sendPacket);
jtf.setText(null);


} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

setTitle("服务器");
setSize(400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try{
socket=new DatagramSocket(8000);
sendPacket=new DatagramPacket(buf, buf.length);
receivePacket=new DatagramPacket(buf, buf.length);

while(true){
Arrays.fill(buf,(byte)0);

socket.receive(receivePacket);

jta.append("The Client host name is "+receivePacket.getAddress().getHostName()+
" and port number is "+receivePacket.getPort()+"\n");

jta.append("客户端:"+new String(buf).trim()+"\n");

sendPacket.setAddress(receivePacket.getAddress());
sendPacket.setPort(receivePacket.getPort());
}
}
catch (Exception e) {
// TODO: handle exception
}
}

public static void main(String[] args){
new ServerUseUDP();
}
}
wh070502219 2010-08-17
  • 打赏
  • 举报
回复
今天反正没事干 就学学socket

刚写了个使用UDP协议的 单线程的聊天室

有兴趣的自己去改成多线程 和我发的第一个代码原理是一样的
liuyuhua0066 2010-08-17
  • 打赏
  • 举报
回复
我插,估计这帖子又得被管理员加精推荐了,无语
evangelionxb 2010-08-17
  • 打赏
  • 举报
回复
有点像马士兵的chat
但是你这个还是有些许可以再改进的地方
楼主比我强多了。
kyousuke 2010-08-17
  • 打赏
  • 举报
回复
写个服务器,让其他人注册登录,找朋友
- -!当然只能是局域网的
wh070502219 2010-08-17
  • 打赏
  • 举报
回复
自己占个座
wh070502219 2010-08-17
  • 打赏
  • 举报
回复

不知道图能不能打开

希望哥哥姐姐们 提意见
wh070502219 2010-08-17
  • 打赏
  • 举报
回复
客户端程序代码


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.print.attribute.standard.Severity;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ClientChat extends JFrame{
private JTextField jtf=new JTextField();
private JTextArea jta=new JTextArea();
private ObjectOutputStream output;
private ObjectInputStream input;
private String string1="服务器:";
private String string2="客服端:";

public ClientChat(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jtf,BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

jtf.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String clientString=null;
clientString=jtf.getText();
jtf.setText(null);
try {
output.writeObject(clientString);
output.flush();
jta.append(string2+clientString+"\n");

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});
setTitle("客户端");
setSize(400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try{
Socket socket=new Socket("localhost", 8000);
InetAddress inet=socket.getInetAddress();

System.out.println("Client's host name:"+inet.getHostName());
System.out.println("Client's IP address:"+inet.getHostAddress());

output=new ObjectOutputStream(socket.getOutputStream());
input=new ObjectInputStream(socket.getInputStream());

while(true){
jta.append(string1+" :"+(String)input.readObject()+"\n");
}
}
catch (Exception e) {
// TODO: handle exception
}
}

public static void main(String[] args){
new ClientChat();
}

}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧