求JAVA webQQ在线聊天源码

最近在写webQQ项目,求类似的JAVAweb用socket编写的程序``... 最近在写webQQ项目,求类似的JAVA web用socket编写的程序`` 展开
 我来答
匿名用户
2013-08-31
展开全部
客户端源代码
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class socketClient
{
private DataOutputStream output;
private DataInputStream input;
private String clientName;

public static void main(String[] args)
{
//在main函数中,启动服务器的socket
new socketClient().ConnectServer();
}

public void ConnectServer()
{
try
{
Socket socket = new Socket("127.0.0.1",10001);
clientName = socket.getInetAddress().toString();
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());

new readServer().start();
new writeServer().start();
}
catch(Exception e) {System.out.println(e.toString());}
}

public class readServer extends Thread
{
private Socket client;

public void run()
{
String msg;
try
{
while(true)
{
msg = input.readUTF();
if(msg!=null)
System.out.println("收到消息:【"+clientName+"】 "+msg);
}
}
catch(Exception e) {System.out.println(e.toString());}
}
}

public class writeServer extends Thread
{
private Socket client;
public void run()
{
try
{
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while(true)
{
if(stdIn.ready())
{
userInput = stdIn.readLine();
if(userInput!="exit")
{
output.writeUTF(userInput);
System.out.println("已发送消息给【"+clientName+"】"+userInput);
}
}
}
}
catch(Exception e) {System.out.println(e.toString());}
}
}

}

服务器源代码

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class socketServer
{
ArrayList clientList = new ArrayList();
//private DataOutputStream output;
//private DataInputStream input;
//private String clientName;
//private Socket socket;

public static void main(String[] args)
{
//在main函数中,启动服务器的socket
new socketServer().OpenServer();

}

public void OpenServer()
{
try
{
ServerSocket server = new ServerSocket(10001);

Socket socket;
while((socket = server.accept())!=null)
{
clientList.add(socket);
//clientName = socket.getInetAddress().toString();
//output = new DataOutputStream(socket.getOutputStream());
//input = new DataInputStream(socket.getInputStream());
new readClient(socket).start();
new writeClient(socket).start();
}
}
catch(Exception e) {System.out.println(e.toString());}
}

public class readClient extends Thread
{
private Socket socket;

public readClient(Socket client)
{socket = client;}

public void run()
{
String msg;
try
{
String clientName = socket.getInetAddress().toString();
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream());
while((msg = input.readUTF())!=null)
{
System.out.println("收到消息:【"+clientName+"】 "+msg);
}
}
catch(Exception e){System.out.println(e.toString());}
}
}

public class writeClient extends Thread
{
private Socket socket;

public writeClient(Socket client)
{socket = client;}

public void run()
{
try{

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
String clientName = socket.getInetAddress().toString();
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream());
while(true)
{
if(stdIn.ready())
{
userInput = stdIn.readLine();
if(userInput!="exit")
{
output.writeUTF(userInput);
System.out.println("已发送消息给【"+clientName+"】"+userInput);
}
}
}
}
catch(Exception e) {System.out.println(e.toString());}
}
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式