java socket,我有客户端和服务器的代码,帮我添加广播和能多人会话,加分!!代码如下

服务器端importjava.net.*;importjava.io.*;publicclasstestServer{publicstaticvoidmain(Strin... 服务器端
import java.net.*;
import java.io.*;
public class testServer {
public static void main(String[] args){
try{
ServerSocket s=new ServerSocket(5050);
Socket ss=s.accept();
InputStream is=ss.getInputStream();
DataInputStream dis=new DataInputStream(is);

OutputStream os=ss.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);

new ServerReader(dis).start();
new ServerWriter(dos).start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
class ServerReader extends Thread{
private DataInputStream dis;
public ServerReader(DataInputStream dis){
this.dis=dis;
}
public void run(){
String input;
try{
while(true){
input=dis.readUTF();
System.out.println("Client:" +input);
if(input.equals("bye"))
{
System.out.println("Other quit...");
System.exit(0);
}
}
}catch(IOException e)
{
e.printStackTrace();
}

}
}
class ServerWriter extends Thread{
private DataOutputStream dos;
public ServerWriter(DataOutputStream dos){
this.dos=dos;
}
public void run(){
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String input;
try{
while(true){
input=br.readLine();
dos.writeUTF(input);
if(input.equals("bye")){
System.out.println("quit...");
System.exit(0);
}
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}

客户端
package example;

import java.io.*;
import java.net.Socket;

public class testClient {
public static void main(String[] args){
try{
Socket ss=new Socket("192.168.1.120",5050);
InputStream is=ss.getInputStream();
DataInputStream dis=new DataInputStream(is);

OutputStream os=ss.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);

new ClientReader(dis).start();
new ClientWriter(dos).start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
class ClientReader extends Thread{
private DataInputStream dis;
public ClientReader(DataInputStream dis){
this.dis=dis;
}
public void run(){
String input;
try{
while(true){
input=dis.readUTF();
System.out.println("Server: "+input);
if(input.equals("bye")){
System.out.println("Other quit...");
System.exit(0);
}
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
class ClientWriter extends Thread{
private DataOutputStream dos;
public ClientWriter(DataOutputStream dos){
this.dos=dos;
}
public void run(){
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String input;
try{
while(true){
input=br.readLine();
dos.writeUTF(input);
if(input.equals("bye")){
System.out.println("quit...");
System.exit(0);
}
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
我要代码。。。。直接添加到我的代码里,或修改,不要全改。。。
展开
 我来答
心随下雨天
2012-03-29
知道答主
回答量:22
采纳率:0%
帮助的人:22.9万
展开全部
根据你的改了个!不好意思,其中读写的思路稍微有点不同!不过可以做参考!

Server端代码:
import java.net.*;
import java.io.*;
import java.util.*;

public class TestServer {
ServerSocket s = null;
boolean started = false;//用来监听服务器是否启动!
List<ServerReaderWriter> clients = new ArrayList<ServerReaderWriter>();//用来存放启动的客服端

public static void main(String[] args) {
new TestServer().start();
}

public void start() {
try {
s = new ServerSocket(5050);
started = true;
} catch(SocketException e) {
System.out.println("5050端口正在使用中!!!请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}

int i = 1;
try {
while(started) {
Socket ss = s.accept();
ServerReaderWriter c = new ServerReaderWriter(ss);//建立客服端
System.out.println("第" + i + "个客服端启动!");
++i;
new Thread(c).start();//启动线程
clients.add(c);
}
} catch (EOFException e) {
System.out.println("客服端被关闭!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
class ServerReaderWriter implements Runnable { //建议使用Runnable避免你重写run方法麻烦!
private Socket s;
private DataInputStream dis = null;//注意赋值,养成好习惯!
private DataOutputStream dos = null;

private boolean bConnected = false;//用于调用连接成功后的run方法
public ServerReaderWriter(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("有客户退出!");
}
}

public void run() {
try {
while (bConnected) {
String input = dis.readUTF();
System.out.println(input);
for(int i=0; i<clients.size(); ++i) {
ServerReaderWriter c = clients.get(i);
c.send(input);
}
}
} catch(SocketException e) {
System.out.println("一个客服端已关闭,请勿再像他发送信息!");
} catch (EOFException e) {
System.out.println("谢谢使用!");
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (s != null) {
s.close();
s = null;
}
//clients.clear();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}

Client端代码:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class TestClient extends Frame { //用到Frame生产界面比较直观
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;

TextField tfText = new TextField();
TextArea taContent = new TextArea();

Thread tRecv = new Thread(new ClientReaderWriter());

public static void main(String[] args){
new TestClient().launchFrame();
}

public void launchFrame() {
this.setSize(300, 300); //设置客服端窗口格式
this.setLocation(400, 300);
add(tfText, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
this.pack();
this.addWindowListener(new WindowAdapter() { //监听窗口关闭事件

public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}

});

tfText.addActionListener(new TFListener());

setVisible(true);
connect();
tRecv.start();
}

public void connect() {
try {
s = new Socket("127.0.0.1", 5050); //依据自己的服务器,我这里用的localhost
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("连接服务器!");
bConnected = true;
} catch(ConnectException e) {
System.out.println("请检查服务器是否启动!");
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.exit(0);
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

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

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String str = tfText.getText().trim();
tfText.setText("");
try {
dos.writeUTF(str);
if(str.equals("bye")){
System.exit(0);
}
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}

}

class ClientReaderWriter implements Runnable {
public void run() {
try {
while(bConnected) {
String input = dis.readUTF();
taContent.setText(taContent.getText() + input +'\n');
}
} catch (SocketException e) {
System.out.println("轻轻的我走了!Bye-bye!");
} catch (EOFException e) {
System.out.println("我断网了,再见!");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
百度网友c86dad5
2012-03-28 · TA获得超过290个赞
知道小有建树答主
回答量:425
采纳率:100%
帮助的人:430万
展开全部
来一个Session的链接,把这个Session链接保存到List中,每收到一个连接的发送消息,转发到List中其他所有的Session中
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
FALWRDFTAK
2012-03-28
知道答主
回答量:3
采纳率:0%
帮助的人:3.1万
展开全部
不懂
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式