如题
以下是代码,为了实现server端和client端的通信,server端能成功运行。
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);
setSize(300,200);
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){}
}
}
求高手帮忙啊。谢谢!!
以下是代码,为了实现server端和client端的通信,server端能成功运行。
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);
setSize(300,200);
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){}
}
}
求高手帮忙啊。谢谢!!
解决方案 »
- 关于socket通信的问题
- 请问这段代码有什么问题
- 请问专家一个关于SocketTimedoutException的问题
- SQLException:[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
- 用,号分割的字符串转成数组
- 这个为什么要Windows 2000 下看不到效果
- 对了,double类型如何比较?:)
- (急)如何得到Apache服务器的本地路径?
- 关于JasperReports生成xml文件的问题?
- 谁有JBuilder5的datastore的key?30分哟!
- 对2个ArrayList集合下的值进行比较~
- 求解释
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
把组件都加上J
省得事多