这是大二学JAVA基础时写的一个小程序,通过在程序中设置主机IP和端口实现两台主机间的通信。由于当时对JAVA编程的规范掌握的不是很好,
所以程序界面设计代码和通信模块代码都放在了同一个类中。
类一:服务器端的代码
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyServer {
JFrame frame;
JButton but;
JButton but1;
JTextField tf;
JTextArea ta;
JPanel pane;
BufferedReader br;
BufferedWriter bw;
public MyServer(){
frame=new JFrame("server");
but=new JButton("send");
but1=new JButton("close");
tf=new JTextField(15);
ta=new JTextArea(5,15);
JPanel pane=new JPanel();
pane.add(but);
pane.add(but1);
Container c=frame.getContentPane();
c.setLayout(new BorderLayout());
c.add(new JScrollPane(ta),BorderLayout.NORTH);
c.add(pane,BorderLayout.CENTER);
c.add(tf,BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
ta.setEnabled(false);
/*
上面部分是界面设计的代码
*/
but.addActionListener(new ActionListener() {//给发送按钮添加事件监听器
public void actionPerformed(ActionEvent event){
try {
if(tf.getText()==null||tf.getText()=="")//判断用户有没有在文本框输入文本
{bw.newLine();//这是生成换行符,由于带有缓存的字符流(例如BufferedWriter)是以换行符作为每一行数据结束的标记,所以
如果没有这一行代码的话,客户端是收不到用户发送的信息的,直到服务器端结束运行。
bw.flush();//这句代码的意思是清空缓存,作用是使用户写入的信息能及时发送出去,由于用户没有输入文本,所以客户端收到的是空格
display("MySelf: " + tf.getText());//在显示框显示发送的信息
}
else
{
bw.write(tf.getText());
bw.newLine();
bw.flush();
display("MySelf: " + tf.getText());
}
tf.setText("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
but1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
public void display(String s){
ta.append(s + "\n");
ta.setCaretPosition(ta.getText().length());
tf.requestFocusInWindow();
}
public void run() throws Exception{
ServerSocket server=new ServerSocket(5000);//对端口5000进行监听
Socket s=server.accept();//开始监听服务
while(true){
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
while(br.read()!=-1){
display("Client: " + br.readLine());
}
s.close();
}
}
public static void main(String[] args) throws Exception{
MyServer app=new MyServer();
app.run();
}
}
类二:客户端的代码,客户端代码和服务器端代码基本相同,只是服务器端多了ServerSocket server=new ServerSocket(5000);
Socket s=server.accept();这两句代码用于提供监听服务
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyClient extends Thread{
JFrame frame;
JButton but;
JTextField tf;
JTextArea ta;
JPanel pane;
BufferedReader br;
BufferedWriter bw;
private JButton but1;
public MyClient(){
frame=new JFrame("Client");
but=new JButton("send");
but1=new JButton("close");
tf=new JTextField(15);
ta=new JTextArea(5,15);
JPanel pane=new JPanel();
pane.add(but);
pane.add(but1);
Container c=frame.getContentPane();
c.setLayout(new BorderLayout());
c.add(new JScrollPane(ta),BorderLayout.NORTH);
c.add(pane,BorderLayout.CENTER);
c.add(tf,BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
ta.setEnabled(false);
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
try {
if(tf.getText()==null||tf.getText()=="")
{bw.newLine();
bw.flush();
display("MySelf: " + tf.getText());
}
else
{
bw.write(tf.getText());
bw.newLine();
bw.flush();
display("MySelf: " + tf.getText());
}
tf.setText("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
but1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
public void display(String s){
ta.append(s + "\n");
ta.setCaretPosition(ta.getText().length());
tf.requestFocusInWindow();
}
public void run(){
Socket s;
try {
s = new Socket("localhost",5000);
while(true){
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
while(br.read()!=-1){
display("Server: " + br.readLine());
}
s.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
MyClient app=new MyClient();
//
app.start();//app.run();
}
}
----------------------------------------------------------------------------------------------------------------------------------
由于这是一年前写的代码,现在对相关的知识只有大概的印象,对细节的东西的表述可能有偏差,欢迎大家纠错
原文:http://www.cnblogs.com/DasionTechnologyBlog/p/4042771.html