求高手看看我的Java程序哪里有问题

mmxuzheng 2009-03-28 10:47:01
这是一个实现服务器和客户端通信的程序,但是运行后客户端不能向服务器发送信息,而且客户端也不能正常关掉。学校的课程设计过两天就要验收了,哪位高手能帮我解决下,万分感谢!!!
客户端:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyClient {
public static void main(String[] args)
{
new ClientFrame();
}

}
class ClientFrame extends Frame implements ActionListener
{
Socket clientSocket;
BufferedReader cIn;
PrintWriter cOut;
String s;
TextArea textArea;
Button myButton=new Button("发送");
public ClientFrame()
{
setTitle("客户端socket窗口");
setLayout(new BorderLayout());
this.addWindowListener(new WinAdptClient(this));
myButton.addActionListener(this);
textArea=new TextArea(20,50);
add("South",myButton);
add("Center",textArea);
setVisible(true);
try{
clientSocket=new Socket("wxy123",8000);
cIn=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
cOut=new PrintWriter(clientSocket.getOutputStream());
s=cIn.readLine();
textArea.append(s+"\n");
}catch(Exception e){}
}
public void actionPerformed(ActionEvent e)
{
try{
cOut.print(textArea.getText());
cOut.flush();
}catch(Exception ex){}
}
}
class WinAdptClient extends WindowAdapter
{
ClientFrame m_Parent;
WinAdptClient(ClientFrame p)
{
m_Parent=p;
}
public void windowClosing(WindowEvent e)
{
try{
m_Parent.cOut.println("Bye");
m_Parent.cOut.flush();
m_Parent.cIn.close();
m_Parent.cOut.close();
m_Parent.clientSocket.close();
m_Parent.dispose();
System.exit(0);
}catch(Exception ex){}
}
}


服务器端:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class MySocketServer {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ServerService(8000,10);
}

}
class ServiceFrame extends Frame implements Runnable
{
ServerService fatherListener;
Socket connectedClient;
TextArea serverMeg;
ServiceFrame(ServerService sv,Socket s)
{
fatherListener=sv;
connectedClient=s;
setTitle("服务器端socket窗口");
setLayout(new BorderLayout());
serverMeg=new TextArea(10,50);
add("Center",serverMeg);
setVisible(true);
InetAddress clientAddress=connectedClient.getInetAddress();
serverMeg.append("Client connected"+" from\n"+clientAddress.toString()+".\n");
}
public void run()
{
try{
BufferedReader sIn=new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));
PrintWriter sOut=new PrintWriter(connectedClient.getOutputStream());
sOut.println("Helo!Wellcome connected to our server!\r");
sOut.flush();
String s=sIn.readLine();
while(!s.equals("Bye"))
{
serverMeg.append("Client端输入的信息为:\n"+s);
s=sIn.readLine();
}
connectedClient.close();
}catch(Exception e){}
fatherListener.addMeg("Client"+"closed."+"\n");
dispose();

} //run()
}
class ServerService extends Frame
{
ServerSocket m_sListener;
TextArea listenerMeg;
public ServerService(int Port,int Count)
{
try{
m_sListener=new ServerSocket(Port,Count);
setTitle("服务器端监听服务窗口");
this.addWindowListener(new WinAdpt());
setLayout(new BorderLayout());
listenerMeg=new TextArea("监听服务器已经启动\n",10,50);
add("Center",listenerMeg);
setVisible(true);
while(true)
{
Socket connected=m_sListener.accept();
InetAddress clientAddress=connected.getInetAddress();
listenerMeg.append("Client"+"connected"+"from\n"+clientAddress.toString()+".\n");
ServiceFrame myST1=new ServiceFrame(this,connected);
Thread myST2=new Thread(myST1);
myST2.start();
}
}catch(IOException e){}
}
public void addMeg(String s)
{
listenerMeg.append(s);
}
}
class WinAdpt extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
(e.getWindow()).dispose();
System.exit(0);
}
}
...全文
28 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复

还没有回复,快来抢沙发~

相关推荐
发帖
Eclipse
创建于2007-09-28

5.8w+

社区成员

Java Eclipse
申请成为版主
帖子事件
创建了帖子
2009-03-28 10:47
社区公告
暂无公告