JAVA聊天服务器代码 java服务端编程
为java聊天室代码加详细注释,并说明设计思路。好的加100分。
import java.io.*;
创新互联建站专注骨干网络服务器租用十载,服务更有保障!服务器租用,托管服务器 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;//引入包。
public class ChatClient {
public static void main(String[] args) {
ChatClient cc = new ChatClient();
cc.receive();
}
JTextField jtf; // 文本条
JTextArea jta; //文本域。
Socket s; //客户端
PrintWriter out; //输出流
BufferedReader in; //输入流
public ChatClient() {
JFrame frame = new JFrame("ChatClient");//窗口
frame.setSize(400, 300); //大小
jta = new JTextArea(); //文本域
jta.setEditable(false); //不可编辑
jtf = new JTextField();//文件条
jtf.addActionListener(new ActionListener() { //添加监听。
public void actionPerformed(ActionEvent arg0) {
send(); //调用send()方法
}
});
frame.getContentPane().add(new JScrollPane(jta)); //添加滚动条
frame.getContentPane().add(jtf, "South"); //添加文本条
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口。
frame.setVisible(true); //可显示的。
try {
s = new Socket("127.0.0.1", 9000); //连接服务端 socket("主机名",端口号);
in = new BufferedReader(new InputStreamReader(s.getInputStream())); //建立输入流
out = new PrintWriter(s.getOutputStream());//输出流
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive() { //接受服务端发来别的客户端的信息。
while (true) {
try {
String text = in.readLine(); //读一行
this.jta.append(text + "\n"); //jta 添加上读入的。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
public void send() { //发送消息
String text = this.jtf.getText(); //得到你输入的消息
this.jtf.setText(""); //在文本域中显示你输入的消息。
out.println(text); //打印出。
out.flush(); //清空
}
}
Server端
import java.net.*;
import java.io.*;
import java.util.*;//引入包
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000); //建立服务端,端口号为9000
List list = new ArrayList(); //创建个List集合。
while (true) {
Socket s = ss.accept(); //等待客户端的请求。
list.add(s); //把每一个client都add到集合中去。
Thread t = new ServerThread(s, list); //线程。
t.start(); //启动。
}
}
}
class ServerThread extends Thread {
Socket s;
List list;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s, List list) { //构造。传入socket和list。
this.s = s;
this.list = list;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream())); //输入流
out = new PrintWriter(s.getOutputStream()); //输出流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() { //必须实现其run()方法。
while (true) {
try {
String str = in.readLine(); //得到client端的message。
if (str == null) //如果没有消息就返回。
return;
Iterator it = list.iterator(); //遍历list。
while (it.hasNext()) { //如果list有下一个
Socket socket = (Socket) (it.next()); //因为list中都是存的socket
PrintWriter o = new PrintWriter(socket.getOutputStream()); //输出流
o.println(str); //输出
o.flush(); //清空
}
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
return;
}
}
}
}
编一个简单的java聊天程序,但是运行时服务器和客户端都出错
给你简单修改了一下,你看看吧
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class ChatClient {
public static void main(String[] args) throws Exception {
Socket c = new Socket("127.0.0.1", 1123);
while (true) {
Date now = new Date();
DateFormat d = DateFormat.getDateTimeInstance();
String time = d.format(now);
PrintWriter OutputS = new PrintWriter(c.getOutputStream());
BufferedReader say = new BufferedReader(new InputStreamReader(System.in));
String Csay = say.readLine();
System.out.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);
OutputS.println("Client:" + String.valueOf(c.getLocalPort()) + Csay + time);
OutputS.flush();
BufferedReader InputS = new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(InputS.readLine());
}
}
}
、、、、、、、、、、、、、、、、
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class ChatServer {
private static int PORTNUM = 1123;// 定义程序使用的端口号PORTNUM
public static void main(String args[]) throws Exception {
ServerSocket s = new ServerSocket(PORTNUM);
while (true) {
Socket s1 = s.accept();
BufferedReader InputS = new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.err.println();
System.out.println(InputS.readLine());
Date now = new Date();
DateFormat d = DateFormat.getDateTimeInstance();
String time = d.format(now);
PrintWriter OutputS = new PrintWriter(s1.getOutputStream());
BufferedReader say = new BufferedReader(new InputStreamReader(System.in));
String Ssay = say.readLine();
OutputS.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);
OutputS.flush();
System.out.println("Server" + String.valueOf(s1.getLocalPort()) + Ssay + time);
}
}
}
说一下你的程序出现的原因吧!
你的客户端与程序段在开始运行都在监听对方发来的消息,当什么都不会有了,因为对方都在等着接受消息呢,这样就造成了死锁
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();
}
}
网页名称:JAVA聊天服务器代码 java服务端编程
浏览路径:http://scjbc.cn/article/doigpch.html