Java之Socket编程之简单聊天程序

本文介绍了一个简单的 Java Socket 聊天室应用程序,包括服务器端和客户端的实现代码。服务器端能够处理多个客户端连接,并将消息广播给所有在线用户。客户端可以发送和接收消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=medium][b]一、界面截图:[/b][/size]

[img]http://dl2.iteye.com/upload/attachment/0122/2836/07352c68-d489-30dd-b739-acb35d5a443b.png[/img]


[size=medium][b]二、代码:[/b][/size]


[b]1、ChatServer.java[/b]


package com.lixh1986.socket.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;

import com.lixh1986.socket.model.ServerSideSocket;

public class ChatServer {

@SuppressWarnings("resource")
public static void main(String[] args) {

ArrayList<ServerSideSocket> pool = new ArrayList<ServerSideSocket>();

try {

ServerSocket serverSocket = new ServerSocket(9999);

System.out.println("Server starts up!");

while(true){
/**
* ServerSocket.accept();
*
Listens for a connection to be made to this socket and accepts it.
The method blocks until a connection is made.

A new Socket corresponding to the client socket is created, and this
socket will be used for making communication with the client socket.

*/
ServerSideSocket serverSideSocket =
new ServerSideSocket(pool, serverSocket.accept());
Thread t = new Thread(serverSideSocket);
System.out.println("socket thread: " + t.getName() + " starts serving.");
t.start();
}


} catch (IOException e) {
e.printStackTrace();
}

}

}




[b]2、ServerSideSocket.java[/b]


package com.lixh1986.socket.model;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

public class ServerSideSocket implements Runnable {

private Socket socket;
private ArrayList<ServerSideSocket> pool;


public ServerSideSocket(ArrayList<ServerSideSocket> pool, Socket socket){
this.pool = pool;
this.socket = socket;
this.pool.add(this);
System.out.println("A client connected!");
}


@Override
public void run() {
receiveMessage();
}

/**
* read data from ClientSideSocket.
*/
private void receiveMessage(){
try {
byte[] bytes = new byte[1024];
int n = 0;
boolean isUserSendQuit = false;
while(!isUserSendQuit){
while ( (n = socket.getInputStream().read(bytes)) != -1){
String str = new String (bytes, 0, n);
// broadcast this message to other client.
for(ServerSideSocket otherSocket : pool){
if(this != otherSocket){
otherSocket.writeMessage(str);
}
}
}
}
} catch (IOException e1) {
System.out.println("receiveMessage:");
System.out.println("A client disconnected!");
}finally{
disconnect();
}
}


/**
* write data to ClientSideSocket.
* @param message
*/
public void writeMessage(String message){
try {
socket.getOutputStream().write(message.getBytes());
socket.getOutputStream().flush();
} catch (IOException e1) {
System.out.println("writeMessage:");
System.out.println("A client disconnected!");
disconnect();
}
}


private void disconnect(){
if(pool.remove(this)){
System.out.println("A client is removed successfully!");
System.out.println("pool size: " + pool.size());
}else{
System.out.println("A client is not removed!");
}
try {
if(socket != null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}





[b]3、ClientSideSocket.java[/b]


package com.lixh1986.socket.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

@SuppressWarnings("resource")
public class ClientSideSocket {

public static void main(String[] args) {
try {

final Socket socket = new Socket("127.0.0.1", 9999);
final Scanner scanner = new Scanner(System.in);

System.out.println("I am a client!");


new Thread(new Runnable() {
@Override
public void run() {
try {
while(true){
String str = scanner.nextLine();
socket.getOutputStream().write(str.getBytes());
socket.getOutputStream().flush();
}
} catch (IOException e) {
System.out.println("Writing Quit.");
System.exit(0);
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] bytes = new byte[1024];
int n = 0;
while(true){
while ( (n = socket.getInputStream().read(bytes)) != -1){
String str = new String (bytes, 0, n);
System.out.println(str);
}
}
} catch (IOException e) {
System.out.println("Reading Quit.");
System.exit(0);
}
}
}).start();



} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


}





[size=medium][b]三、Java Socket通信原理图[/b][/size]

[img]http://dl2.iteye.com/upload/attachment/0122/2907/06ccd7b1-4c86-3115-8b5f-580de61f15d3.png[/img]


[size=medium][b]四、Java Socket 编程进阶[/b][/size]


[url=http://stackoverflow.com/questions/17206570][b]Will I need a separate socket and thread for every player that joins? [JAVA][/b][/url]
服务器端是否应该为每一个客户端创建一个线程?

I have been learning about sockets for sometime now (I'm quite young) and I think I have a good grip on java sockets. I have decided to create a simple multiplayer Java 2D social game. My goal is to have the server output players' X,Y coordinates and chat every 10 milliseconds. From what I have read, my very average logic tells me that only one user at a time can connect to a socket. So therefore I will need a separate thread and socket for each player that connects.

Is it necessary to have one ServerSocket and thread per player?


[b]Answer:[/b]

You should have just one ServerSocket listening on a port that is known to the client. When a client connects to the server, a new Socket object is created and the original ServerSocket goes back to listening again. You should then spin off a new Thread or hand over to an Executor the actual work of talking to the client, otherwise your server will stop listening for client connections.

Here is a very basic sketch of the code you will need.



import java.net.*;
import java.util.concurrent.*;

public class CoordinateServer {
public static void main(String... argv) throws Exception {
// 'port' is known to the server and the client
int port = Integer.valueOf(argv[0]);
ServerSocket ss = new ServerSocket(port);

// You should decide what the best type of service is here
ExecutorService es = Executors.newCachedThreadPool ();

// How will you decide to shut the server down?
while (true) {
// Blocks until a client connects, returns the new socket
// to use to talk to the client
Socket s = ss.accept ();

// CoordinateOutputter is a class that implements Runnable
// and sends co-ordinates to a given socket; it's also
// responsible for cleaning up the socket and any other
// resources when the client leaves
es.submit(new CoordinateOutputter(s));
}
}
}




I have put sockets here since they are easier to get started with, but once you have this working well and want to boost your performance you will probably want to investigate the java.nio.channels package.

There's a good [url=https://www.ibm.com/developerworks/java/tutorials/j-nio/]tutorial over at IBM[/url].


-
转载请注明,
原文出处: http://lixh1986.iteye.com/blog/2348001


-
java聊天程序源码 2 需求分析 2.1 业务需求 1. 与聊天室成员一起聊天。 2. 可以与聊天室成员私聊。 3. 可以改变聊天内容风格。 4. 用户注册(含头像)、登录。 5. 服务器监控聊天内容。 6. 服务器过滤非法内容。 7. 服务器发送通知。 8. 服务器踢人。 9. 保存服务器日志。 10.保存用户聊天信息。 2.2 系统功能模块 2.2.1 服务器端 1.处理用户注册 2.处理用户登录 3.处理用户发送信息 4.处理用户得到信息 5.处理用户退出 2.2.2 客户端 1.用户注册界面及结果 2.用户登录界面及结果 3.用户发送信息界面及结果 4.用户得到信息界面及结果 5.用户退出界面及结果 2.3 性能需求 运行环境:Windows 9x、2000、xp、2003,Linux 必要环境:JDK 1.5 以上 硬件环境:CPU 400MHz以上,内存64MB以上 3.1.2 客户端结构 ChatClient.java 为客户端程序启动类,负责客户端的启动和退出。 Login.java 为客户端程序登录界面,负责用户帐号信息的验证与反馈。 Register.java 为客户端程序注册界面,负责用户帐号信息的注册验证与反馈。 ChatRoom.java 为客户端程序聊天室主界面,负责接收、发送聊天内容与服务器端的Connection.java 亲密合作。 Windowclose 为ChatRoom.java的内部类,负责监听聊天室界面的操作,当用户退出时返回给服务器信息。 Clock.java 为客户端程序的一个小程序,实现的一个石英钟功能。 3. 2 系统实现原理 当用户聊天时,将当前用户名、聊天对象、聊天内容、聊天语气和是否私聊进行封装,然后与服务器建立Socket连接,再用对象输出流包装Socket的输出流将聊天信息对象发送给服务器端 当用户发送聊天信息时,服务端将会收到客户端用Socket传输过来的聊天信息对象,然后将其强制转换为Chat对象,并将本次用户的聊天信息对象添加到聊天对象集Message中,以供所有聊天用户访问。 接收用户的聊天信息是由多线程技术实现的,因为客户端必须时时关注更新服务器上是否有最新消息,在本程序中设定的是3秒刷新服务器一次,如果间隔时间太短将会增加客户端与服务器端的通信负担,而间隔时间长就会让人感觉没有时效性,所以经过权衡后认为3秒最佳,因为每个用户都不可能在3秒内连续发送信息。 当每次用户接收到聊天信息后将会开始分析聊天信息然后将适合自己的信息人性化地显示在聊天信息界面上。 4.1.1 问题陈述 1.接受用户注册信息并保存在一个基于文件的对象型数据库。 2.能够允许注册过的用户登陆聊天界面并可以聊天。 3.能够接受私聊信息并发送给特定的用户。 4.服务器运行在自定义的端口上#1001。 5.服务器监控用户列表和用户聊天信息(私聊除外)。 6.服务器踢人,发送通知。 7.服务器保存日志。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值