难道就真的没有高手解决这个问题了的吗??再加100分!! http://java.sun.com/products/java-media/ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 whuise (情磁场): 好累!整整调试了二十分钟! 你的服务器端程序没有问题,但客户端程序的确是编译通过而不能运行。这里我就对客户端程序说一下: 在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){} }} 如何定位某个xml元素的后一个元素? 如何降低CPU占用率? java单例模式怎么释放资源 为了java有没有必要安装Linux? 各位大虾问下 socket问题 初写程序,高手指点!! 关于Thinking in java这本书中的问题!高手进来! 请教几个关于JATable的问题? 急急我的IE5为何不能运行java2的applet呀,java2的插件我也装了,但一打开java2 的applet ie5就关闭!!急 一个关于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){}
}
}