当前位置: 代码迷 >> Java相关 >> 客户端与服务端自由通信
  详细解决方案

客户端与服务端自由通信

热度:442   发布时间:2006-12-02 23:19:38.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("????????????");
}
}
}


----------------解决方案--------------------------------------------------------
哇,强
----------------解决方案--------------------------------------------------------
刚运行了一下下,真不错,顶一下
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

收藏了,,


----------------解决方案--------------------------------------------------------
  效果很好,一点儿错误都没,不像我在网上找的其它程序改错就要改半天,谢谢啦
----------------解决方案--------------------------------------------------------
  相关解决方案