我用JAVA写了一个简单的聊天程序,大概的功能为,一个服务器端,多个客户端可以去链接,并且,客户端向服务端发送消息,服务端接收之后将这个消息显示并且发到每一个客户端上,但是不知道该怎么实现发送到每一个客户端的功能,代码如下,请指教-------------------------服务器端---------------------------------package src;import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Frame{
TextArea ta = new TextArea("Server online");
ServerSocket ss = null;
//DataInputStream dis = null;
String sendStr = null;
int number = 0;
public static void main(String[] args) {
new chatServer().launchFrame();
} void launchFrame()
{
setLocation(300,300);
setSize(300,300);
setVisible(true);
add(ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
conneted();
}
//此方法用于连接客服端,并实现数据的传输
void conneted()
{
try
{
ss = new ServerSocket(8888);
while(true)
{
Socket s = ss.accept();
new Thread(new threadClient(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//设置多线程以实现多个客户端同时连接服务器
class threadClient implements Runnable
{
private Socket c =null;
public threadClient(Socket s)
{
this.c = s;
}
public void run()
{
try
{
String tstr = null;
ta.append(++number+"computer is conneted"+"\n");
new Thread(new threadsend(c,tstr)).start();
DataInputStream dis = new DataInputStream(c.getInputStream());
while(true)
{
tstr = dis.readUTF();
ta.append(tstr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置多线程实现对多个客户端的消息发送
class threadsend implements Runnable
{
private Socket c =null;
private String s = null;
private String temp = null;
public threadsend(Socket s,String str)
{
this.c = s;
this.s=str;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
while(true)
{
if(temp!=s)
{
dos.writeUTF(s);
temp=s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
--------------------------------客户端--------------------------------package src;import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Frame{
TextArea ta = new TextArea("Server online");
ServerSocket ss = null;
//DataInputStream dis = null;
String sendStr = null;
int number = 0;
public static void main(String[] args) {
new chatServer().launchFrame();
} void launchFrame()
{
setLocation(300,300);
setSize(300,300);
setVisible(true);
add(ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
conneted();
}
//此方法用于连接客服端,并实现数据的传输
void conneted()
{
try
{
ss = new ServerSocket(8888);
while(true)
{
Socket s = ss.accept();
new Thread(new threadClient(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//设置多线程以实现多个客户端同时连接服务器
class threadClient implements Runnable
{
private Socket c =null;
public threadClient(Socket s)
{
this.c = s;
}
public void run()
{
try
{
String tstr = null;
ta.append(++number+"computer is conneted"+"\n");
new Thread(new threadsend(c,tstr)).start();
DataInputStream dis = new DataInputStream(c.getInputStream());
while(true)
{
tstr = dis.readUTF();
ta.append(tstr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置多线程实现对多个客户端的消息发送
class threadsend implements Runnable
{
private Socket c =null;
private String s = null;
private String temp = null;
public threadsend(Socket s,String str)
{
this.c = s;
this.s=str;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
while(true)
{
if(temp!=s)
{
dos.writeUTF(s);
temp=s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Frame{
TextArea ta = new TextArea("Server online");
ServerSocket ss = null;
//DataInputStream dis = null;
String sendStr = null;
int number = 0;
public static void main(String[] args) {
new chatServer().launchFrame();
} void launchFrame()
{
setLocation(300,300);
setSize(300,300);
setVisible(true);
add(ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
conneted();
}
//此方法用于连接客服端,并实现数据的传输
void conneted()
{
try
{
ss = new ServerSocket(8888);
while(true)
{
Socket s = ss.accept();
new Thread(new threadClient(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//设置多线程以实现多个客户端同时连接服务器
class threadClient implements Runnable
{
private Socket c =null;
public threadClient(Socket s)
{
this.c = s;
}
public void run()
{
try
{
String tstr = null;
ta.append(++number+"computer is conneted"+"\n");
new Thread(new threadsend(c,tstr)).start();
DataInputStream dis = new DataInputStream(c.getInputStream());
while(true)
{
tstr = dis.readUTF();
ta.append(tstr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置多线程实现对多个客户端的消息发送
class threadsend implements Runnable
{
private Socket c =null;
private String s = null;
private String temp = null;
public threadsend(Socket s,String str)
{
this.c = s;
this.s=str;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
while(true)
{
if(temp!=s)
{
dos.writeUTF(s);
temp=s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
--------------------------------客户端--------------------------------package src;import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class chatServer extends Frame{
TextArea ta = new TextArea("Server online");
ServerSocket ss = null;
//DataInputStream dis = null;
String sendStr = null;
int number = 0;
public static void main(String[] args) {
new chatServer().launchFrame();
} void launchFrame()
{
setLocation(300,300);
setSize(300,300);
setVisible(true);
add(ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
conneted();
}
//此方法用于连接客服端,并实现数据的传输
void conneted()
{
try
{
ss = new ServerSocket(8888);
while(true)
{
Socket s = ss.accept();
new Thread(new threadClient(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//设置多线程以实现多个客户端同时连接服务器
class threadClient implements Runnable
{
private Socket c =null;
public threadClient(Socket s)
{
this.c = s;
}
public void run()
{
try
{
String tstr = null;
ta.append(++number+"computer is conneted"+"\n");
new Thread(new threadsend(c,tstr)).start();
DataInputStream dis = new DataInputStream(c.getInputStream());
while(true)
{
tstr = dis.readUTF();
ta.append(tstr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置多线程实现对多个客户端的消息发送
class threadsend implements Runnable
{
private Socket c =null;
private String s = null;
private String temp = null;
public threadsend(Socket s,String str)
{
this.c = s;
this.s=str;
}
public void run() {
try {
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
while(true)
{
if(temp!=s)
{
dos.writeUTF(s);
temp=s;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
解决方案 »
- 修改类文件,tomcat自动重启报错,请教高手
- linux vsftp 文件上传
- jsp中的request值为null Struts
- 如何根据wsdl,自动生成html页面
- tomcat发布时出现错误
- [求助]想学用Hibernate,在配置过程中出现了点问题,请帮忙
- javamail问题,请各位帮我调通它,很简单,编译就能用,一个测试javamail的application,100分送上,绝不食言
- struts-config.xml大疑问?(散分!!!)
- 各位推荐一下把java做的程序生成windows下exe文件的工具
- 在线等待,焦急!!
- no socket available
- 用java+jdbc连接了access数据库,这样的东西可以放在linux上运行么?谢谢
②Server在accept接到用户连接请求的时候,将此连接的OutputStream放入Server对象中某List成员变量中。
③负责往客户端发消息的方法遍历以上List,往每个OutputStream中写入消息,完成发送。