62,628
社区成员
发帖
与我相关
我的任务
分享
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();
}
}



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();
}
}
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();
}
}

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();
}
}