//serverAgent.java
import java.net.ServerSocket;
import java.net.Socket;
public class ServerAgent
{
java.io.DataInputStream input=null;
java.io.DataOutputStream output=null; ServerAgent(int port) //main()函数传递监听端口号
{
System.out.println("服务器代理正在监听,端口:" + port);
ServerSocket svrSkt=null;
Socket cltSkt=null;
try
{
svrSkt = new ServerSocket(port); //开始监听
}
catch (Exception e)
{
System.out.println("监听端口" + port + "失败");
}
try
{
cltSkt = svrSkt.accept(); //接收连接请求
System.out.println("got connection!");
}
catch (Exception e)
{
System.out.println("连接失败");
}
try
{
output = new java.io.DataOutputStream(cltSkt.getOutputStream()); //获得输出流
input = new java.io.DataInputStream(cltSkt.getInputStream()); //获得输入流// output.writeUTF("欢迎......");
// output.flush();
}
catch (Exception e)
{
e.printStackTrace();
} } public String getRequest()
{
String frmClt = null;
try
{
frmClt = input.readUTF();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("无法读取端口.....");
System.exit(0);
}
return frmClt;
} public void sendResponse(String response)
{
try
{
output.writeUTF(response);
output.flush();
}
catch (Exception e)
{
System.out.println("写端口失败......");
e.printStackTrace();
System.exit(0);
}
} public static void main(String[] args) throws Exception
{
ServerAgent sa = new ServerAgent(1001);
while (true)
{
sa.sendResponse(sa.getRequest());
}
}
}
import java.net.ServerSocket;
import java.net.Socket;
public class ServerAgent
{
java.io.DataInputStream input=null;
java.io.DataOutputStream output=null; ServerAgent(int port) //main()函数传递监听端口号
{
System.out.println("服务器代理正在监听,端口:" + port);
ServerSocket svrSkt=null;
Socket cltSkt=null;
try
{
svrSkt = new ServerSocket(port); //开始监听
}
catch (Exception e)
{
System.out.println("监听端口" + port + "失败");
}
try
{
cltSkt = svrSkt.accept(); //接收连接请求
System.out.println("got connection!");
}
catch (Exception e)
{
System.out.println("连接失败");
}
try
{
output = new java.io.DataOutputStream(cltSkt.getOutputStream()); //获得输出流
input = new java.io.DataInputStream(cltSkt.getInputStream()); //获得输入流// output.writeUTF("欢迎......");
// output.flush();
}
catch (Exception e)
{
e.printStackTrace();
} } public String getRequest()
{
String frmClt = null;
try
{
frmClt = input.readUTF();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("无法读取端口.....");
System.exit(0);
}
return frmClt;
} public void sendResponse(String response)
{
try
{
output.writeUTF(response);
output.flush();
}
catch (Exception e)
{
System.out.println("写端口失败......");
e.printStackTrace();
System.exit(0);
}
} public static void main(String[] args) throws Exception
{
ServerAgent sa = new ServerAgent(1001);
while (true)
{
sa.sendResponse(sa.getRequest());
}
}
}
public class ClientAgent
{
java.io.DataInputStream ips = null;
java.io.DataOutputStream ops = null; public ClientAgent(String serverName, int port)
{
try
{
java.net.Socket clientSocket = new java.net.Socket(serverName, port); //根据服务器名和端口号建立Socket
ops = new java.io.DataOutputStream(clientSocket.getOutputStream()); //获得Socket的输出流
ips = new java.io.DataInputStream(clientSocket.getInputStream()); //获得Socket的输入流
}
catch (Exception e)
{
System.out.println("无法连接服务器!");
}
} public void sendRequest(String request)
{
try
{
ops.writeUTF(request); //向Socket的输出流写入字符串
ops.flush();
}catch(Exception ex)
{
ex.printStackTrace();
}
} public String getResponse()
{
String str = new String();
try
{
str = ips.readUTF(); //从Socket的输入流读入字符串
}
catch (Exception e)
{
e.printStackTrace();
} //必须捕获错误
return str;
} public static void main(String[] args)
{
AppClient frame = new AppClient();
//Center the window
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true); ClientAgent ca = new ClientAgent("127.0.0.1", 1001); //传递服务器名称和端口号
frame.setClientAgent( ca );
}
}//javaClient.java---被改名为AppClient.java
package com.test.csdn.socket;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//javaClient.java
public class AppClient
extends JFrame
implements ActionListener //以JFrame为基类,实现ActionListener接口
{
ClientAgent ca;
JButton sendButton; //"发送"按钮
JTextField inputField; // 输入框
JTextArea outputArea; // 服务器返回框
public AppClient() //在建构函数中完成图形界面的初始化
{
JPanel contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout()); //Center the window
this.setSize(new Dimension(500, 500));
this.setTitle("Frame Title"); inputField = new JTextField("这里输入..."); //供客户端输入的文本框
outputArea = new JTextArea("服务器返回"); //显示服务器返回数据的文本域
sendButton = new JButton("发送");
sendButton.addActionListener(this);
JPanel panel = new JPanel(); //新建面板
panel.setLayout(new BorderLayout()); //设置面板风格为BorderLayout
panel.add(inputField, BorderLayout.NORTH); //放置控件
panel.add(outputArea, BorderLayout.CENTER);
panel.add(sendButton,BorderLayout.SOUTH);
setTitle("Java通讯客户端");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(panel,BorderLayout.CENTER); // setVisible(true); }
public void setClientAgent(ClientAgent ca)
{
this.ca = ca;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sendButton) //判断事件源控件是否是"发送"按钮
{
System.out.println("will send data!");
ca.sendRequest(inputField.getText()); //发送文本框中的文本
outputArea.append("\r\n" + ca.getResponse()); //接收服务器回应并写入文本域
}
}}