速求用JAVA语言写聊天室的源代码

【ClientSocketDemo.java 客户端Java源代码】import java.net.*;import java.io.*;public class ClientSocketDemo { //声明客户端Socket对象socket Socket socket = null;//声明客户器端数据输入输出流 DataInputStream in;DataOutputStream out;//声明字符串数组对象response,用于存储从服务器接收到的信...
速求用JAVA语言写聊天室的源代码
【ClientSocketDemo.java 客户端Java源代码】

import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//声明客户端Socket对象socket
Socket socket = null;

//声明客户器端数据输入输出流
DataInputStream in;
DataOutputStream out;

//声明字符串数组对象response,用于存储从服务器接收到的信息
String response[];

//执行过程中,没有参数时的构造方法,本地服务器在本地,取默认端口10745
public ClientSocketDemo()
{
try
{
//创建客户端socket,服务器地址取本地,端口号为10745
socket = new Socket("localhost",10745);

//创建客户端数据输入输出流,用于对服务器端发送或接收数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());

//获取客户端地址及端口号
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());

//向服务器发送数据
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);

//从服务器接收数据
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}

//执行过程中,有一个参数时的构造方法,参数指定服务器地址,取默认端口10745
public ClientSocketDemo(String hostname)
{
try
{
//创建客户端socket,hostname参数指定服务器地址,端口号为10745
socket = new Socket(hostname,10745);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);

response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}

//执行过程中,有两个个参数时的构造方法,第一个参数hostname指定服务器地址
//第一个参数serverPort指定服务器端口号
public ClientSocketDemo(String hostname,String serverPort)
{
try
{
socket = new Socket(hostname,Integer.parseInt(serverPort));
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);

response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}

public static void main(String[] args)
{
String comd[] = args;
if(comd.length == 0)
{
System.out.println("Use localhost(127.0.0.1) and default port");
ClientSocketDemo demo = new ClientSocketDemo();
}
else if(comd.length == 1)
{
System.out.println("Use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println("Hostname and port are named by user");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);
}
else System.out.println("ERROR");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

【ServerSocketDemo.java 服务器端Java源代码】

import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//声明ServerSocket类对象
ServerSocket serverSocket;

//声明并初始化服务器端监听端口号常量
public static final int PORT = 10745;

//声明服务器端数据输入输出流
DataInputStream in;
DataOutputStream out;

//声明InetAddress类对象ip,用于获取服务器地址及端口号等信息
InetAddress ip = null;

//声明字符串数组对象request,用于存储从客户端发送来的信息
String request[];

public ServerSocketDemo()
{
request = new String[3]; //初始化字符串数组
try
{
//获取本地服务器地址信息
ip = InetAddress.getLocalHost();

//以PORT为服务端口号,创建serverSocket对象以监听该端口上的连接
serverSocket = new ServerSocket(PORT);

//创建Socket类的对象socket,用于保存连接到服务器的客户端socket对象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);

//创建服务器端数据输入输出流,用于对客户端接收或发送数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());

//接收客户端发送来的数据信息,并显示
request[0] = in.readUTF();
request[1] = in.readUTF();
request[2] = in.readUTF();
System.out.println("Received messages form client is:");
System.out.println(request[0]);
System.out.println(request[1]);
System.out.println(request[2]);

//向客户端发送数据
out.writeUTF("Hello client!");
out.writeUTF("Your ip is:"+request[1]);
out.writeUTF("Your port is:"+request[2]);
}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
ServerSocketDemo demo = new ServerSocketDemo();
}
}2009-12-20
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
/*
*聊天室服务的主框架类
*/
public class ChatServer extends JFrame implements ActionListener{
public static int port=8888;//服务端的侦听端口
ServerSocket serverSocket;//服务端Socket
Image icon;//程序图标
JComboBox combabox;//选择发送消息的接受者
JTextArea messageShow;//服务端的信息显示
JScrollPane messageScrollPane;//信息的滚动条
JtextField showStatus;//显示用户连接状态
JLabel sendToLabel,messageLabel;
JTextField sysMessage;//服务端消息的发送
JButton sysMessageButton;//服务器消息的发送按钮
UserLinkList userLinkList;//用户链表
//建立菜单栏
JMenuBar jMenuBar=new JMenuBar();
//建立菜单组
JMenu serviceMenu=new JMenu("服务(V)");
//建立菜单项
JMenuItem portItem=new JMenuItem("端口设置(p)");
JMenuItem startItem=new JMenuItem("启动服务(s)");
JMenuItem stopItem=new JMenuItem("停止服务(T)");

JMenu helpMenu=JMenu("帮助(H)");
JMenuItem helpMenu=new JMenuItem("帮助(H)");
//建立工具栏
JToolBar toolBar=new JToolBar;

//建立工具栏中的按钮
JButton portSet;//启动服务端侦听
JButton startServer;//启动服端务侦听
JButton stopServer;//关闭服务端侦听
JButton exitButton;//退出按钮

//框架的大小
Dimension faceSize=new Dimension(400,600);

SeverListenThresd ListenThresd;

JPanel downPanel;
GridBagLayout gridBag;
GridBagConstraints gridBagCon;

/**
*服务端构造函数
*/
public ChatServer()
init();//初始化程序

//添加框架的关闭事件处理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(faceSize);

//设置运行时窗口的位置
Dimension screenSize=
Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(screenSize.width-faceSize())
/2,(int)(screenSize.height-faceSize.getHeight())/2);
this.setResizable(false);

this.setTitle("聊天室服务端");//设置标题

//程序图标
icon=getImage("icon.gif");
this.setIconImage(icon);//设置程序图标
show();

//为服务菜单栏设置热键'V'
serviceMenu.setMnemonic('V')

//为端口设置快捷键Ctrl+P
portItem.setMnemonic('P');
portItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_P,InputEvent.CTRL_MASK));

//为启动服务快捷键为Ctrl+S
startItem.setMnemonic('S');
startItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_S,InputEvent.CTRL_MASK));

//为端口设置快捷键Ctrl+T
stopItem.setMnemonic('T');
stopItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_T,InputEvent.CTRL_MASK));

//为退出设置快捷键为Ctrl+X
exitItem.setMnemonic('X');
exitItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_X,InputEvent.CTRL_MASK));

//为帮助菜单栏设置热键'H'
helpMenu.setMnemonic('H');

//为帮助设置快捷键为Ctrl+H
helpItem.setMnemonic('p');
helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));

}

/**
*程序初始化函数
*/
public void init(){

Container contentPane=getContentPane();
contentPane.setLayout(new BoraderLayout());
//添加菜单栏
serviceMenu.add(portItem);
serviceMenu.add(startItem);
serviceMenu.add(stopItem);
serviceMenu.add(exitItem);
jMenuBar.add(serviceMenu);
helpMenu.add(helpItem);
jMenubar.add(helpMenu);
setJmenuBar(jMenuBar);

//初始化按钮
portSet=new JButton("端口服务");
startServer=new JButton("启动服务");
stopServer=new JButton("停止服务");
exitButton=new JButton("退出");
//将按钮添加到工具栏
toolBar.add(portSet);
toolBar.addSeparator();//添加分隔栏
toolBar.add(starServer);
toolBar.add(stopServer);
toolBar.addSeparator();//添加分隔栏
toolBar.add(exitButton);
contentPane.add(toolBar,BorderLayout.NORTH);

//初始时,令停止服务按钮不可用
stopServer.setEnabled(false);
stopItem.setEnabled(false);

//为菜单栏添加事件监听
portItem.addActionListener(this);
startItem.addActionListener(this);
stopItem.addActionListener(this);
exitItem.addActionListener(this);
helpItem.addActionListener(this);

//添加按钮的事件侦听
portSet.addActionListener(this);
startServer.addActionListener(this);
stopServer.addActionListener(this);
exitButton.addActionListener(this);

combobox=new JComboBox();
conbobox.inserItemAt("所有人",0);
combobox.setSelectedIndex(0);

messageShow=new JTextArea();
messageShow.setEditable(false);//添加滚动条
messageScrollpane =new JScrollPane(messageShow,
JScorellpane.VETICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_AS_NEEDED);2009-12-22
不懂swing..呵呵.不过JSP也可以做2009-12-19
嘿。。居然一个班的。。连老师示例的字都一样。。2009-12-28
mengvlog 阅读 10 次 更新于 2025-07-19 12:16:04 我来答关注问题0
  • 【ClientSocketDemo.java 客户端Java源代码】import java.net.*;import java.io.*;public class ClientSocketDemo { //声明客户端Socket对象socket Socket socket = null;//声明客户器端数据输入输出流 DataInputStream in;DataOutputStream out;//声明字符串数组对象response,用于存储从服务器接收到的信...

  •  百度网友d7acbd0aa java实现聊天室是怎么做到的?

    Java 实现聊天室可以分为以下几个步骤:建立服务器端 首先需要建立一个服务器端,负责接收客户端的连接请求并处理客户端发送过来的消息。建立客户端 然后需要建立客户端,客户端通过网络连接到服务器端,并向服务器端发送消息。实现通信协议 为了实现聊天室功能,需要定义一个通信协议,规定客户端和服务器端...

  •  lengreen1221 求用Java编写的聊天室界面

    jsp的 欢乐聊天室 < String chatid = new String();chatid...

  •  青春影视剪辑 求一个用java socket编写的聊天室程序,能运行的附带源码,有客户端和服务器端

    服务端:import java.io.*;import java.net.*;import java.util.*;public class ChatServer { boolean started = false;ServerSocket ss = null;List clients = new ArrayList();public static void main(String[] args) { new ChatServer().start();} public void start() {...

  •  lame_chen 怎么实现java一对一聊天室?可以介绍一下吗

    要实现 Java 的一对一聊天室,可以使用 Socket 编程实现基于 TCP 协议的通信。以下是一些大致的步骤:创建服务器端程序:创建一个服务器端程序,等待客户端的连接请求。在连接请求到来时,服务器会创建一个新的线程来处理这个连接请求。创建客户端程序:创建一个客户端程序,连接服务器端程序。在连接成功...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部