服务器端代码是:
package chat_room;import java.io.*;
import java.net.*;public class ChatRoomServer {
private ServerSocket serverSocket; private int port; public ChatRoomServer(int port) {
this.port = port;
} public void talk() {
try {
serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
Thread t = new ServerThread(socket);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
new ChatRoomServer(8000).talk();
}}class ServerThread extends Thread {
private Socket socket; public ServerThread(Socket socket) {
this.socket = socket;
} public BufferedReader getIS(Socket socket) {
BufferedReader br = null;
InputStream is = null;
try {
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
return br; } public PrintWriter getOS(Socket socket) {
OutputStream os = null;
PrintWriter pw = null;
try {
os = socket.getOutputStream();
pw = new PrintWriter(os, true);
} catch (IOException e) {
e.printStackTrace();
}
return pw;
} public void run() {
BufferedReader br = this.getIS(socket);
PrintWriter pw = this.getOS(socket);
String temp = null;
String data = "";
try {
while (true) {
// while (!(temp = br.readLine()).equals("bye")) {
while ((temp = br.readLine())!=null) {
pw.println(temp);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}客房端是:package chat_room;import java.io.*;
import java.net.*;public class ChatRoomClient {
private Socket socket; private String host; private int port; public ChatRoomClient(String host, int port) {
this.host = host;
this.port = port;
} public BufferedReader getIS(Socket socket) {
BufferedReader br = null;
InputStream is = null;
try {
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
return br;
} public PrintWriter getOS(Socket socket) {
OutputStream os = null;
PrintWriter pw = null;
try {
os = socket.getOutputStream();
pw = new PrintWriter(os, true);
} catch (IOException e) {
e.printStackTrace();
}
return pw;
} public void talk() {
BufferedReader brInput = new BufferedReader(new InputStreamReader(
System.in));
BufferedReader br = null;
PrintWriter pw = null;
try {
socket = new Socket(host, port);
br = this.getIS(socket);
pw = this.getOS(socket);
String temp = null;
String data = "";
// while (true) {
// while (true) {
// if ((temp = brInput.readLine()).equals("bye")) {
// data += temp + '\n';
// break;
// }
// temp = brInput.readLine();
// data += temp + '\n';
// }
// pw.println(data);
// pw.flush();
// String tempIn = null;
// String dataIn = ""; // 从server得到的数据
// while ((tempIn = br.readLine()) != null) {
// dataIn += tempIn + '\n';
// }
// System.out.println(dataIn);
// } while (true) {
while (!(temp = brInput.readLine()).equals("bye")) {
System.out.println("exec");
data += temp + '\n';
}
pw.println(data);
pw.flush();
String tempIn = null;
String dataIn = ""; // 从server得到的数据
while ((tempIn = br.readLine()) != null) {
System.out.println(tempIn);
}
brInput.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
new ChatRoomClient("localhost", 8000).talk();
}
}
为什么这个程序只能执行一次的通信.
而不能循环的通信?
package chat_room;import java.io.*;
import java.net.*;public class ChatRoomServer {
private ServerSocket serverSocket; private int port; public ChatRoomServer(int port) {
this.port = port;
} public void talk() {
try {
serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
Thread t = new ServerThread(socket);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
new ChatRoomServer(8000).talk();
}}class ServerThread extends Thread {
private Socket socket; public ServerThread(Socket socket) {
this.socket = socket;
} public BufferedReader getIS(Socket socket) {
BufferedReader br = null;
InputStream is = null;
try {
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
return br; } public PrintWriter getOS(Socket socket) {
OutputStream os = null;
PrintWriter pw = null;
try {
os = socket.getOutputStream();
pw = new PrintWriter(os, true);
} catch (IOException e) {
e.printStackTrace();
}
return pw;
} public void run() {
BufferedReader br = this.getIS(socket);
PrintWriter pw = this.getOS(socket);
String temp = null;
String data = "";
try {
while (true) {
// while (!(temp = br.readLine()).equals("bye")) {
while ((temp = br.readLine())!=null) {
pw.println(temp);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}客房端是:package chat_room;import java.io.*;
import java.net.*;public class ChatRoomClient {
private Socket socket; private String host; private int port; public ChatRoomClient(String host, int port) {
this.host = host;
this.port = port;
} public BufferedReader getIS(Socket socket) {
BufferedReader br = null;
InputStream is = null;
try {
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
return br;
} public PrintWriter getOS(Socket socket) {
OutputStream os = null;
PrintWriter pw = null;
try {
os = socket.getOutputStream();
pw = new PrintWriter(os, true);
} catch (IOException e) {
e.printStackTrace();
}
return pw;
} public void talk() {
BufferedReader brInput = new BufferedReader(new InputStreamReader(
System.in));
BufferedReader br = null;
PrintWriter pw = null;
try {
socket = new Socket(host, port);
br = this.getIS(socket);
pw = this.getOS(socket);
String temp = null;
String data = "";
// while (true) {
// while (true) {
// if ((temp = brInput.readLine()).equals("bye")) {
// data += temp + '\n';
// break;
// }
// temp = brInput.readLine();
// data += temp + '\n';
// }
// pw.println(data);
// pw.flush();
// String tempIn = null;
// String dataIn = ""; // 从server得到的数据
// while ((tempIn = br.readLine()) != null) {
// dataIn += tempIn + '\n';
// }
// System.out.println(dataIn);
// } while (true) {
while (!(temp = brInput.readLine()).equals("bye")) {
System.out.println("exec");
data += temp + '\n';
}
pw.println(data);
pw.flush();
String tempIn = null;
String dataIn = ""; // 从server得到的数据
while ((tempIn = br.readLine()) != null) {
System.out.println(tempIn);
}
brInput.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
new ChatRoomClient("localhost", 8000).talk();
}
}
为什么这个程序只能执行一次的通信.
而不能循环的通信?
你看看这个吧import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;class Client {
public static void main(String[] args) {
MyFrame fram = new MyFrame();
}
}class MyFrame extends Frame {
Panel p1;
Panel p2;
Panel p3;
TextArea show;
TextField input;
Label port;
Label ip;
//Label name;
TextField tPort;
TextField tIp;
//TextField tName;
Button submit;
Button login;
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnect = false; public MyFrame() {
init();
}
public void init() {
/***********************************show begin****************************************************/
p1 = new Panel();
show = new TextArea(15,80);
show.setEditable(false);
p1.add(show);
add(p1,BorderLayout.CENTER);
/***********************************show end****************************************************/ /***********************************Panel2 begin****************************************************/
p2 = new Panel(new FlowLayout(FlowLayout.LEFT));
port = new Label("PORT");
tPort = new TextField(7);
ip = new Label("IP");
tIp = new TextField(25);
//name = new Label("NAME");
//tName = new TextField(7);
login = new Button("login");
p2.add(port);
p2.add(tPort);
p2.add(ip);
p2.add(tIp);
//p2.add(name);
//p2.add(tName);
p2.add(login);
login.addActionListener(new submitAction());
add(p2,BorderLayout.NORTH);
/***********************************Panel2 end****************************************************/
/***********************************Panel3 begin****************************************************/
p3 = new Panel(new FlowLayout(FlowLayout.LEFT));
input = new TextField(70);
submit = new Button("submit");
p3.add(input);
p3.add(submit);
input.addActionListener(new inputAction());
submit.addActionListener(new submitAction());
add(p3,BorderLayout.SOUTH);
/***********************************Panel3 end****************************************************/
/********************************fram begin************************************************/
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
//disConnect();
System.exit(0);
}
});
this.setSize(300, 300);
setLocation(100,100);
pack();
setVisible(true);
/********************************fram end************************************************/
}
class submitAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ("submit".equals(e.getActionCommand())) {
String str = input.getText().trim();
//show.setText(show.getText() + str + '\n');
input.setText("");
faSong(str);
}else if ("login".equals(e.getActionCommand())) {
connect();
}
}
}
/****************************************回车输出事件*****************************************************/
class inputAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = input.getText().trim();
//show.setText(show.getText() + str + '\n');
input.setText("");
faSong(str);
}
}
/*******************************向服务端发送信息*******************************************************/
public void faSong(String str)
{
try
{
dos.writeUTF(str);
dos.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*******************************关闭资源*******************************************************/
/*public void disConnect()
{
try
{
dos.close();
}
catch (IOException e)
{
//e.printStackTrace();
System.out.println("系统错误");
}
}*/
/*******************************************接收服务器发送的信息******************************************************/
private class RecvClient implements Runnable
{
public void run()
{
try
{
while (bConnect)
{
String str = dis.readUTF();
show.setText(show.getText()+str+'\n');
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*******************************connection begin*******************************************************/
public void connect() {
try
{
s = new Socket(tIp.getText(),Integer.parseInt(tPort.getText()));
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bConnect = true;
new Thread(new RecvClient()).start();
System.out.println("我连进来啦!~哈哈~~");
}
catch (UnknownHostException uhe)
{
uhe.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;class Server {
boolean started = false;
ServerSocket ss = null;
List<ClientRun> list = new ArrayList<ClientRun>();
public static void main(String[] args) {
new Server().start();
} public void start()
{
try
{
ss = new ServerSocket(8888);
started = true;
}
catch (BindException be)
{
System.out.println("端口使用中!");
}
catch (IOException e)
{
System.out.println("服务器连接失败!");
}
try
{
while (started) {
Socket s = ss.accept();
ClientRun cr = new ClientRun(s); //主线程只负责接收信息,每个客户端连接进来都会开始一个新线程
new Thread(cr).start(); //把连接进来的Socket传到线程中。
list.add(cr);
System.out.println("系统提示:"+s.getInetAddress()+"已经联入");
}
}
catch (IOException e)
{
System.out.println("Client closed!");
}
}
/***********************************多线程实现多客户端同时连接**************************************************/
class ClientRun implements Runnable
{
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
boolean bConnect = false;
public ClientRun(Socket s)
{
this.s = s;
try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnect = true;
}
catch (IOException e)
{
e.printStackTrace();
}
} public void send(String str)
{
try
{
dos.writeUTF(str);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void run()
{
try
{
while (bConnect)
{
String str = dis.readUTF();
System.out.println(str);
for (int i=0 ;i<list.size() ;i++ )
{
ClientRun cr = list.get(i);
cr.send(str);
}
}
}
catch (Exception e)
{
System.out.println(s.getInetAddress()+"离开了!");
}
finally
{
try
{
if (dis != null) dis.close();
if (dos != null) dos.close();
if (s != null){
s.close();
s = null;
}
}
catch (IOException io)
{
io.printStackTrace();
}
}
}
}
}
不就可以了。
import java.io.*;
public class ClientThread extends Thread{//接Server 数据
private PrintWriter out = null;
private Socket s = null;
private BufferedReader in;
private String str=null;
private QQClientFrame QQ=null;
public ClientThread(Socket s,QQClientFrame QQ){
this.s = s;
this.QQ=QQ;
}
public void run(){
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(),true);
out.println(str);
while(true){
str=in.readLine();
if(str.equals("bye")){
str="client from Server1"+str;
out.println(str);
QQ.txtInfoShow.append(str+"\n");
break;
}else{
str="client from Server2"+str;
out.println(str);
QQ.txtInfoShow.append(str+"\n");
}
this.stop();
}
in.close();
out.close();
s.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} }}
-----------------------------------------------------------
package qqserver;
import java.net.*;
import java.io.*;
public class oneServerThread extends Thread{//发送数据
private Socket s;
private QQServerFrame QQ;
public oneServerThread(Socket s,QQServerFrame QQ) {
this.s=s;
this.QQ=QQ;
}
public void run(){
try{
String info=QQ.txtUserSend.getText();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in,info));
PrintWriter out=new PrintWriter(s.getOutputStream());
while(true){
String str=in.readLine();
out.println(str);
QQ.txtInfoShow.append(str+"\n");
if(str.equals("bye")){
break;
}
}
in.close();
out.close();
}catch(Exception se){
se.printStackTrace();
}
}
}