cleey
望着那一丝海线,若隐若现。落日下的海霞,数不尽的美,看不完的醉
java即时通信多线程,socket
这是一个关于socket多线程编程的实例,笔者使用java实现了这个简单的过程:
贴出来大家一起探讨下;
分为服务端和客户端:代码在下面;可以直接运行;
服务端:CLServer.java

package hello;
import java.net.*;
import java.util.*;
import java.io.*;
public class CLServer
{
int portid = 10000; // 服务端端口号
boolean isstarted = false; // 服务器开始监听
ServerSocket srvsocket = null; // 服务端Socket
List<Client> clients = new ArrayList<Client>(); // 声明一个数组存储所有的socket

// 主函数
public static void main(String[] args)
{
new CLServer().start();
}

// 开始监听工作
public void start()
{
try{
srvsocket = new ServerSocket(portid); // 实例化serversocket
isstarted = true;
System.out.println("服务器启动成功!端口:"+portid+"\n ***开始监听***");
}catch(Exception b){
System.out.println("服务器端错误:端口使用中");
}

try{
int clnum = 1; // 客户端的socket数量
while(isstarted)
{
Socket clsocket = srvsocket.accept();
Client c = new Client(clsocket);
System.out.println("第"+clnum+"个加入群聊,ip:"+clsocket.getInetAddress()+",端口号:"+clsocket.getPort());
clnum++;
new Thread(c).start();
clients.add(c);
}
}catch(IOException e){
System.out.println("离开了一个");
}finally{
try{
srvsocket.close();
}catch(IOException w){
w.printStackTrace();
}
}
}

class Client implements Runnable
{
private Socket cs = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean cconn = false;

public Client(Socket s)
{
cs = s;
try{
dis = new DataInputStream(cs.getInputStream()); // 读取信息
dos = new DataOutputStream(cs.getOutputStream()); // 发送信息
cconn = true;
}catch(IOException e){
e.printStackTrace();
}
}

public void sendmsg(String str)
{
try{
dos.writeUTF(str);
}catch(IOException e){
clients.remove(this);
System.out.println("一个client退出了");
}
}

public void run(){
Client cl = null;
try {
while(cconn){
String str = cs.getPort()+ ":" + dis.readUTF(); // 得到信息发给每个人
System.out.println(str);
for(int i = 0;i<clients.size();i++) // 挨个发送群聊信息
{
cl = clients.get(i);
cl.sendmsg(str);
}
}
}catch(Exception se){
System.out.println("有一人离开了聊天室");
}finally{
try{
if(dis != null){dis.close();}
if(dos != null){dos.close();}
if(cs != null){cs.close();}
}catch(IOException ioe){
System.out.println("关闭时:IOException 错误");
}
}
}
}
}


客户端: CLClient.java

package hello;
import java.net.Socket;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class CLClient extends Frame
{
TextField tf = null; // 文本框
TextArea ta = null; // 文本域

String srvip="127.0.0.1";
int srvport = 10000; // 服务端端口号
Socket s = null; // 客户端套接字
DataInputStream dis = null; // 接受信息
DataOutputStream dos = null; // 发送信息
boolean clconn = false; // 连接状态

// 主函数
public static void main(String[] args){
new CLClient().LaunchFrame();
}


// 构件Frame
public void LaunchFrame()
{
setLocation(100,100);
setSize(400,300);
tf = new TextField();
ta = new TextArea();
add(tf,BorderLayout.SOUTH);
add(ta,BorderLayout.NORTH);
pack();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
disconnect();
System.exit(0);
}
});
tf.addActionListener(new TFListener());
setVisible(true);
connect();

new Thread(new RcvThread()).start();
}

public void connect(){
try
{
s = new Socket(srvip,srvport);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
ta.setText("*****欢迎加入 CL 聊天室*****\n");
clconn = true;
}catch (IOException e)
{
e.printStackTrace();
}
}

public void disconnect(){
try{
dos.close();
s.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}

// 发出信息
class TFListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
String str =tf.getText();
tf.setText("");
try{
dos.writeUTF(str);
System.out.println("你说:"+str);
dos.flush();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}

// 接收信息
private class RcvThread implements Runnable
{
public void run(){
try {
while(clconn){
String str = dis.readUTF();
ta.setText(ta.getText()+str+"\n");
}
}catch(Exception a){
System.out.println("你退出了聊天室");
}
}
}
}


如有疑问,联系本人
<< 上一篇 facebook之review工具phabricator安装步骤 c语言库文件.h 下一篇 >>
文章标签
随意 | Created At 2014 By William Clinton | 蜀ICP备14002619号-4 |