我的服务器端程序是采用多线程的方式来处理客户端的请求的,但是当客户端发送来字符串以后,服务器端 的线程可以启动,但是就是读不出 客户端发送的数据,一直弄不懂是怎么回事,我的服务器端代码如下:
import java.io.*;
import java.net.*;
import java.util.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Serverside {
ServerSocket SSsocket;
Socket socket;
Vector vet=new Vector();
public Serverside(){
try{
SSsocket=new ServerSocket(4445);
System.out.println("服务已经启动....");
}catch(IOException ioe){
System.out.println("IOE:"+ioe.getMessage());
System.out.println("服务终止!");
System.exit(-1);
}
while(true){
try{
BufferedReader br;
socket=SSsocket.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(IOException ioe){
System.out.println("ioe:"+ioe.getMessage());
}
vet.addElement(socket);
multiThread multh=new multiThread(vet,socket);
multh.start();
// new multiThread(vet,socket).start();
}
}
public static void main(String[] args){
new Serverside();
}
}class multiThread extends Thread{
Vector vet;
Socket socket;
BufferedReader in;
PrintWriter pw;
String line;
Enumeration enu;
Socket client;
boolean running = true;
public multiThread(Vector vet,Socket socket){
this.vet=vet;
this.socket=socket;
enu=vet.elements();
System.out.println("测试1");
}
public void run(){
try{
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.println("测试2");
// line=in.readLine();
// System.out.println("LINE:"+line);
}catch(IOException ioe){
System.out.println("ioE:"+ioe.getMessage());
}
while(running){
System.out.println("测试3");
try{
line=in.readLine();//服务器端侦听到来自客户端的连接以后到了这里为什么读不数据出来?
System.out.println("收到消息:"+line);
System.out.println("测试4");//这里无法打印
}catch(IOException ioe){
System.out.println("读错误:"+ioe.getMessage());
}
if(line.equalsIgnoreCase("end")){
enu=vet.elements();
while(enu.hasMoreElements()){
if(socket==((Socket)enu.nextElement())){
vet.removeElement(socket);
break;
}
}
running=false;//结束当前线程
}
else{
if(line!=null&&!line.equalsIgnoreCase("RegistToServer")){
while(enu.hasMoreElements()){
client=(Socket)enu.nextElement();
try{
pw=new PrintWriter(client.getOutputStream(),true);
pw.println(client.getInetAddress().getHostName()+":");
pw.println(line);
}catch(IOException ioe){
System.out.println("内循环:"+ioe.getMessage());
}
}
}
}
}
try{
if(in!=null)
in.close();
if(pw!=null)
pw.close();
if(client!=null)
client.close();
if(socket!=null)
socket.close();
}catch(IOException ioe){
System.out.println("关闭线程:"+ioe.getMessage());
}
}
}客户端代码如下:import java.awt.*;
import java.awt.event.*;import javax.swing.*;import java.io.*;
import java.net.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientSide extends JFrame{
JTextArea JEPdis=new JTextArea();
JTextArea JEPinput=new JTextArea();
JButton JBsend=new JButton("Send");
JPanel priJP=new JPanel(new BorderLayout());
JPanel botJP=new JPanel(new GridLayout(2,1));//底部2行1列
JPanel botJP1=new JPanel(new BorderLayout());
JPanel botJP2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JScrollPane jsc1;
JScrollPane jsc2;
BufferedReader in;
PrintWriter pw;
Socket socket;
String line;
boolean reading=true;
public ClientSide(){
init();
}
public void init(){
setPaneLayout();//面板布局设置
ConnetToServer();//连接到服务器
AddListentor();//添加监听
}
public void ConnetToServer(){
try{
socket=new Socket("shanghai",4445);
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream());
pw.println("RegistToServer");//在服务器上面注册
}catch(UnknownHostException ioe){
System.out.println("法连接到主机:"+ioe.getMessage());
System.exit(-1);
}catch(IOException ukh){
System.out.println("客户端ukh:"+ukh.getMessage());
System.exit(-1);
}
// ReadFromServer();
}
public void ReadFromServer(){
while(reading){
try{
if(in!=null){
line=in.readLine();
JEPdis.append(line);
}
}catch(IOException iot){
System.out.println("客户端2:"+iot.getMessage());
iot.printStackTrace();
System.exit(-1);
}
}
}
public void setPaneLayout(){
jsc1=new JScrollPane(JEPdis);
jsc2=new JScrollPane(JEPinput);
JEPdis.setEditable(false);
getContentPane().add(priJP);
priJP.add(jsc1,BorderLayout.CENTER);
priJP.add(botJP,BorderLayout.SOUTH);
botJP.add(botJP1);
botJP.add(botJP2);
botJP1.setBackground(Color.RED);
botJP1.add(jsc2,BorderLayout.CENTER);
botJP2.add(JBsend);
setTitle("java群聊");
setSize(600,400);
setVisible(true);
}
public void AddListentor(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
reading=false;
pw.println("end");
if(in!=null)
try{
in.close();
}catch(IOException ioe){
System.out.println("客户端关闭in:"+ioe.getMessage());
}
if(pw!=null)
pw.close();
if(socket!=null)
try{
socket.close();
}catch(IOException ioe){
System.out.println("客户端关闭socket:"+ioe.getMessage());
}
System.exit(0);
}
});
JBsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(JEPinput.getText()!="")
pw.println(JEPinput.getText());
}
});
}
public static void main(String[] args){
ClientSide cs=new ClientSide();
cs.ReadFromServer();
}
}
import java.io.*;
import java.net.*;
import java.util.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Serverside {
ServerSocket SSsocket;
Socket socket;
Vector vet=new Vector();
public Serverside(){
try{
SSsocket=new ServerSocket(4445);
System.out.println("服务已经启动....");
}catch(IOException ioe){
System.out.println("IOE:"+ioe.getMessage());
System.out.println("服务终止!");
System.exit(-1);
}
while(true){
try{
BufferedReader br;
socket=SSsocket.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(IOException ioe){
System.out.println("ioe:"+ioe.getMessage());
}
vet.addElement(socket);
multiThread multh=new multiThread(vet,socket);
multh.start();
// new multiThread(vet,socket).start();
}
}
public static void main(String[] args){
new Serverside();
}
}class multiThread extends Thread{
Vector vet;
Socket socket;
BufferedReader in;
PrintWriter pw;
String line;
Enumeration enu;
Socket client;
boolean running = true;
public multiThread(Vector vet,Socket socket){
this.vet=vet;
this.socket=socket;
enu=vet.elements();
System.out.println("测试1");
}
public void run(){
try{
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.println("测试2");
// line=in.readLine();
// System.out.println("LINE:"+line);
}catch(IOException ioe){
System.out.println("ioE:"+ioe.getMessage());
}
while(running){
System.out.println("测试3");
try{
line=in.readLine();//服务器端侦听到来自客户端的连接以后到了这里为什么读不数据出来?
System.out.println("收到消息:"+line);
System.out.println("测试4");//这里无法打印
}catch(IOException ioe){
System.out.println("读错误:"+ioe.getMessage());
}
if(line.equalsIgnoreCase("end")){
enu=vet.elements();
while(enu.hasMoreElements()){
if(socket==((Socket)enu.nextElement())){
vet.removeElement(socket);
break;
}
}
running=false;//结束当前线程
}
else{
if(line!=null&&!line.equalsIgnoreCase("RegistToServer")){
while(enu.hasMoreElements()){
client=(Socket)enu.nextElement();
try{
pw=new PrintWriter(client.getOutputStream(),true);
pw.println(client.getInetAddress().getHostName()+":");
pw.println(line);
}catch(IOException ioe){
System.out.println("内循环:"+ioe.getMessage());
}
}
}
}
}
try{
if(in!=null)
in.close();
if(pw!=null)
pw.close();
if(client!=null)
client.close();
if(socket!=null)
socket.close();
}catch(IOException ioe){
System.out.println("关闭线程:"+ioe.getMessage());
}
}
}客户端代码如下:import java.awt.*;
import java.awt.event.*;import javax.swing.*;import java.io.*;
import java.net.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientSide extends JFrame{
JTextArea JEPdis=new JTextArea();
JTextArea JEPinput=new JTextArea();
JButton JBsend=new JButton("Send");
JPanel priJP=new JPanel(new BorderLayout());
JPanel botJP=new JPanel(new GridLayout(2,1));//底部2行1列
JPanel botJP1=new JPanel(new BorderLayout());
JPanel botJP2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JScrollPane jsc1;
JScrollPane jsc2;
BufferedReader in;
PrintWriter pw;
Socket socket;
String line;
boolean reading=true;
public ClientSide(){
init();
}
public void init(){
setPaneLayout();//面板布局设置
ConnetToServer();//连接到服务器
AddListentor();//添加监听
}
public void ConnetToServer(){
try{
socket=new Socket("shanghai",4445);
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream());
pw.println("RegistToServer");//在服务器上面注册
}catch(UnknownHostException ioe){
System.out.println("法连接到主机:"+ioe.getMessage());
System.exit(-1);
}catch(IOException ukh){
System.out.println("客户端ukh:"+ukh.getMessage());
System.exit(-1);
}
// ReadFromServer();
}
public void ReadFromServer(){
while(reading){
try{
if(in!=null){
line=in.readLine();
JEPdis.append(line);
}
}catch(IOException iot){
System.out.println("客户端2:"+iot.getMessage());
iot.printStackTrace();
System.exit(-1);
}
}
}
public void setPaneLayout(){
jsc1=new JScrollPane(JEPdis);
jsc2=new JScrollPane(JEPinput);
JEPdis.setEditable(false);
getContentPane().add(priJP);
priJP.add(jsc1,BorderLayout.CENTER);
priJP.add(botJP,BorderLayout.SOUTH);
botJP.add(botJP1);
botJP.add(botJP2);
botJP1.setBackground(Color.RED);
botJP1.add(jsc2,BorderLayout.CENTER);
botJP2.add(JBsend);
setTitle("java群聊");
setSize(600,400);
setVisible(true);
}
public void AddListentor(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
reading=false;
pw.println("end");
if(in!=null)
try{
in.close();
}catch(IOException ioe){
System.out.println("客户端关闭in:"+ioe.getMessage());
}
if(pw!=null)
pw.close();
if(socket!=null)
try{
socket.close();
}catch(IOException ioe){
System.out.println("客户端关闭socket:"+ioe.getMessage());
}
System.exit(0);
}
});
JBsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(JEPinput.getText()!="")
pw.println(JEPinput.getText());
}
});
}
public static void main(String[] args){
ClientSide cs=new ClientSide();
cs.ReadFromServer();
}
}
{
try{
BufferedReader br;
socket=SSsocket.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(IOException ioe){
System.out.println("ioe:"+ioe.getMessage());
}这里有点问题吧,写成Socket socket=SSsocket.accept();
试一下,我没试过
所以根本没发出数据,server自然没接收到.
2.server端采用BufferReader ,readline方法接收,如果传递过来的字符没有回车,这个位置将hang住
3.你的代码,这个位置已经拿了输入流,在线程中又拿一次,是否这个也是原因?基础太差,不敢确定,建议删除这句
while(true){
try{
BufferedReader br;
socket=SSsocket.accept();
-----------------------
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
-----------------------
}catch(IOException ioe){
System.out.println("ioe:"+ioe.getMessage());
}
JBsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(JEPinput.getText()!="")
pw.println(JEPinput.getText());
pw.flush();
}
});
就行了。
现在客户端是可以接受到数据,但是当客户端第二次发送数据时,服务器端的enu.hasMoreElements()就变成了false,这是为什么,还有,客户端的JFrame添加了窗口监听,可是单击关闭按钮却毫无反映,这有时为什么?我重新修改了客户端的代码如下:
import java.awt.*;
import java.awt.event.*;import javax.swing.*;import java.io.*;
import java.net.*;
/**
* @author tadpole_1981
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientSide extends JFrame{
JTextArea JEPdis=new JTextArea();
JTextArea JEPinput=new JTextArea();
JButton JBsend=new JButton("Send");
JPanel priJP=new JPanel(new BorderLayout());
JPanel botJP=new JPanel(new GridLayout(2,1));//底部2行1列
JPanel botJP1=new JPanel(new BorderLayout());
JPanel botJP2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JScrollPane jsc1;
JScrollPane jsc2;
BufferedReader in;
PrintWriter pw;
Socket socket;
String line;
boolean reading=true;
public ClientSide(){
init();
}
public void init(){
setPaneLayout();//面板布局设置
ConnetToServer();//连接到服务器
AddListentor();//添加监听
}
public void ConnetToServer(){
try{
socket=new Socket("shanghai",4445);
in=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream());
pw.println("RegistToServer");//在服务器上面注册
pw.flush();
}catch(UnknownHostException ioe){
System.out.println("法连接到主机:"+ioe.getMessage());
System.exit(-1);
}catch(IOException ukh){
System.out.println("客户端ukh:"+ukh.getMessage());
System.exit(-1);
}
// ReadFromServer();
}
public void ReadFromServer(){
while(reading){
try{
if(in!=null){
line=in.readLine();
JEPdis.append(line);
}
}catch(IOException iot){
System.out.println("客户端2:"+iot.getMessage());
iot.printStackTrace();
System.exit(-1);
}
}
}
public void setPaneLayout(){
jsc1=new JScrollPane(JEPdis);
jsc2=new JScrollPane(JEPinput);
JEPdis.setEditable(false);
getContentPane().add(priJP);
priJP.add(jsc1,BorderLayout.CENTER);
priJP.add(botJP,BorderLayout.SOUTH);
botJP.add(botJP1);
botJP.add(botJP2);
botJP1.setBackground(Color.RED);
botJP1.add(jsc2,BorderLayout.CENTER);
botJP2.add(JBsend);
setTitle("java群聊");
setSize(600,400);
setVisible(true);
}
public void AddListentor(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
reading=false;
pw.println("end");
System.out.println("test客户端关闭");
if(in!=null)
try{
in.close();
}catch(IOException ioe){
System.out.println("客户端关闭in:"+ioe.getMessage());
}
System.out.println("test客户端关闭1");
if(pw!=null)
pw.close();
System.out.println("test客户端关闭2");
if(socket!=null)
try{
socket.close();
}catch(IOException ioe){
System.out.println("客户端关闭socket:"+ioe.getMessage());
}
System.out.println("test客户端关闭3");
System.exit(0);
}
});
JBsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(JEPinput.getText()!=""){
pw.println(JEPinput.getText());
pw.flush();
}
}
});
}
public static void main(String[] args){
ClientSide cs=new ClientSide();
cs.ReadFromServer();
}
}
服务器端的每个发送语句后面都加了pw.flush();