标题:客户端与服务端自由通信
只看楼主
super523996
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-2
 问题点数:0 回复次数:7 
客户端与服务端自由通信
              我的程序里只能让客户端发信息,服务端收,怎么让它们之间自由的通信,不受顺序的限制啊,比如说:在客户端不发送或写信息的情况下,服务端可以写或发送信息:
搜索更多相关主题的帖子: 服务端 客户端 通信 自由 
2006-12-02 23:19
super523996
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-2
得分:0 
各位帮帮忙啊
2006-12-02 23:20
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
得分:0 

MultiServer.java


package cgs;

import java.io.*;
import java.net.*;
public class MultiServer
{ static int clientnum=0;
public static void main(String args[]) throws IOException
{
ServerSocket serverSocket=null;
boolean listening=true;

try{
serverSocket=new ServerSocket(4700);
}
catch(IOException e)
{
System.out.println("Could not listen on port:4700.");
System.exit(-1);
}
while(listening)
{
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++;
}
serverSocket.close();
}
}


ServerThread.java

package cgs;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread
{
private Frame f;
private Panel p;
private TextArea l, t;
private Button b;
private static PrintWriter os;
private static BufferedReader is;
Socket socket = null;
int clientnum;
public ServerThread(Socket socket,int num)
{
this.socket = socket;
clientnum = num+1;
}
public void run()
{
try{
is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
os =new PrintWriter(socket.getOutputStream());
String initstr=is.readLine();
if(!initstr.equalsIgnoreCase(""))
{
f = new Frame("Chat Client");
l = new TextArea(" Welcome to this room!\t已经有"
+clientnum+"人了。\n Client: "+initstr,5,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
p = new Panel();
t = new TextArea("",1,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
b = new Button("Send");

p.setLayout(new GridLayout(1,2));
p.add(t);
p.add(b);
f.add(l,"Center");
f.add(p,"South");

f.setTitle("Chat Server");
f.setBounds(400, 200, 300,100);
f.setVisible(true);
t.requestFocus();

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
f.setVisible(false);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(!t.getText().equalsIgnoreCase("bye")&&!t.getText().equalsIgnoreCase("")){
String line = t.getText();
os.println(line);
os.flush();
l.append("\n Server: "+line);
t.setText("");
t.requestFocus();
}else if(t.getText().equalsIgnoreCase("")){
l.append("\n不允许发空消息!");
}else{
System.exit(1);
}
}
});

String rel;
while(!(rel=is.readLine()).equalsIgnoreCase(""))
{
f.setVisible(true);
l.append("\n Client: "+rel);
t.requestFocus();
}
}
}catch(Exception e){
System.exit(1);
}
}
}


CClient.java

package cgs;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class CClient
{
private Frame f;
private Panel p;
private TextArea l, t;
private Button b;
private static Socket client;
private static PrintWriter os;
private static BufferedReader is;

public static void main(String[] args)
{
new CClient();
}
CClient()
{
try {
client = new Socket("127.0.0.1",4700);
os = new PrintWriter(client.getOutputStream());
is = new BufferedReader(new InputStreamReader(client.getInputStream()));;
f = new Frame("Chat Client");
l = new TextArea(" Welcome to this room!",5,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
p = new Panel();
t = new TextArea("",1,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
b = new Button("Send");

p.setLayout(new GridLayout(1,2));
p.add(t);
p.add(b);
f.add(l,"Center");
f.add(p,"South");

f.setTitle("Chat Client");
f.setBounds(100, 100, 300,100);
f.setVisible(true);
t.requestFocus();

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(1);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(!t.getText().equalsIgnoreCase("bye")&&!t.getText().equalsIgnoreCase("")){
String line = t.getText();
os.println(line);
os.flush();
l.append("\n Client: "+line);
t.setText("");
t.requestFocus();
}else if(t.getText().equalsIgnoreCase("")){
l.append("\n不允许发空消息!");
}else{
System.exit(1);
}
}
});
String rel;
while(!(rel=is.readLine()).equals(""))
{
l.append("\n Server: "+rel);
t.requestFocus();
}
} catch (UnknownHostException e) {
System.out.println("UnknownHost");
} catch (IOException e) {
System.out.println("????????????");
}
}
}


广告位招租
2006-12-03 00:53
阅逡
Rank: 1
等 级:新手上路
帖 子:103
专家分:0
注 册:2006-12-1
得分:0 
哇,强

2006-12-03 01:19
阅逡
Rank: 1
等 级:新手上路
帖 子:103
专家分:0
注 册:2006-12-1
得分:0 
刚运行了一下下,真不错,顶一下

2006-12-03 01:27
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
得分:0 

嘿嘿 当时老师讲的时候给了一个程序 我也有跟楼主一样的想法 于是就改成了整个


广告位招租
2006-12-03 02:22
witchery
Rank: 1
来 自:西安
等 级:新手上路
帖 子:205
专家分:0
注 册:2005-8-6
得分:0 

收藏了,,

2006-12-03 22:17
super523996
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-2
得分:0 
  效果很好,一点儿错误都没,不像我在网上找的其它程序改错就要改半天,谢谢啦
2006-12-07 22:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-106878-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.383358 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved