http://java.sun.com/products/java-media/
解决方案 »
- NaN是什么啊?为什么Double.NaN != Double.NaN啊?
- 纯java工程关于jar包的问题
- 请问关于import java.class.*的问题.......
- java的图像操作
- 想打开一个网页,点了按钮之后却没一点反应?
- 请问各位:用java的FileWriter类对文件进行写操作,如果写的时候硬盘空间满,如何能得到硬盘已满的出错信息?
- jsp程序的运行!!!
- eclipse编译RMI的问题
- 我不明白,程序是怎么给要显示的对象设置不同颜色参数的?
- 在windows 下使用bat 脚本运行jar包 怎么给jar 自定义一个进程名?
- 一个关于server socket factory的问题
- 若用Java开发B/S,一个新手大概花多长时间熟悉
好累!整整调试了二十分钟!
你的服务器端程序没有问题,但客户端程序的确是编译通过而不能运行。这里我就对客户端程序说一下:
在MyClient类中,你写了一个构造函数:public void MyClient(),不知你注意没有,构造函数是不可随便设定类型的,而这里你设为void类型,于是主程序并没有调用该构造函数,这可以通过在构造函数第一句加一个输出测试语句来证明。
解决的办法是,把public void MyClient()
改为public MyClient()
一切解决了!!
在你的程序中,有不少地方是有警告的,而且在客户端的窗口并没有监听,即窗口不能关闭,所以最好加几句用以监听窗口的代码(鉴于时间,我不加上去了)。
随便说一句,如果你的JDK是1.2版本以上的话,完全可以运行这个程序,没必要去换编译器,只是为了正确性,那几处警告要注意一下。
最后,我把端户的源程序再复写一遍,有问题再跟我联系:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class MyClient extends Frame implements ActionListener
{
Socket ClientSocket;
PrintStream os;
DataInputStream is;
String s;
Label MyLabel=new Label("welcome to our host services");
TextArea textArea;
Button myButton=new Button("send");
public MyClient()
{
setTitle("Client Window");
setLayout(new BorderLayout());
this.addWindowListener(new WinAdptClient(this));
myButton.addActionListener(this);
textArea=new TextArea(20,50);
add("North",MyLabel);
add("South",myButton);
add("Center",textArea);
setResizable(false);
pack();
show();
connect();
}
public void connect()
{
try{
ClientSocket=new Socket("Ym",8000);
os=new PrintStream(new BufferedOutputStream(ClientSocket.getOutputStream()));
is=new DataInputStream(new BufferedInputStream(ClientSocket.getInputStream()));
s=is.readLine();
textArea.appendText(s+"\n");
}
catch(Exception e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==myButton)
{
try{
os.print(textArea.getText());
os.flush();
}
catch(Exception ex){}
}
}
public static void main(String args[])
{
new MyClient();
}
}
class WinAdptClient extends WindowAdapter
{
MyClient m_Parent;
WinAdptClient(MyClient p)
{
m_Parent=p;
}
public void windowClosing(WindowEvent e)
{
try{
m_Parent.os.println("Bye");
m_Parent.os.flush();
m_Parent.is.close();
m_Parent.os.close();
m_Parent.ClientSocket.close();
m_Parent.dispose();
System.exit(0);
}
catch(Exception ex){}
}
}