网络编程
1)半双工
服务器
MyServer.java
<span style="font-size:18px;">import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
MyServer server = new MyServer();
}
public MyServer() {
super();
// TODO Auto-generated constructor stub
try {
//在9999号监听
ServerSocket ss = new ServerSocket(9999);
System.out.println("我是服务器,在9999端口监听");
Socket s = ss.accept();
//System.out.println("11");
//要读取数据s中传递的数据
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
String info = br.readLine();
System.out.println("服务器接收到:" + info);
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println("这里是服务器,你好!");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}</span>
客户端
TestClient.java
<span style="font-size:18px;">package TestClient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TestClient {
public TestClient(){
try {
Socket s = new Socket("127.0.0.1",9999);
//如果s连接成功,皆可以发送数据给服务器
//通过pw,想s写数据,true表示及时刷新。
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println("这里是客户端!");
//要读取数据s中传递的数据
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
String info = br.readLine();
System.out.println("客户端接收到:" + info);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
public static void main(String[] args) {
TestClient client = new TestClient();
}
}</span>
可以控制台交流
TestClient2.java
<span style="font-size:18px;">package TestClient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TestClient2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestClient2 client2 = new TestClient2();
}
public TestClient2() {
//连接服务器端
try {
Socket s = new Socket("127.0.0.1", 9999);
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
InputStreamReader isr2 = new InputStreamReader(s.getInputStream());
BufferedReader br2 = new BufferedReader(isr2);
while(true){
//客户端从控制台接受
System.out.println("请输入你想对服务器说的话");
String info = br.readLine();
//发送给服务器
pw.println(info);
if ( info.equals("bye")) {
System.out.println("对话结束");
s.close();
break;
}
//接收服务器发来的话
String res = br2.readLine();
System.out.println("服务器说:"+ res);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
</span>
TestServer2.java
<span style="font-size:18px;">package TestServer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestServer2 server2 = new TestServer2();
}
public TestServer2() {
super();
// TODO Auto-generated constructor stub
try {
//在9999端口监听
System.out.println("客户端在监听9999……");
ServerSocket ss = new ServerSocket(9999);
//等待连接
Socket s = ss.accept();
//先接受客户端发来的信息
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
//接收控制台的谢谢
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader br2 = new BufferedReader(isr2);
while (true) {
String infoFromClient = br.readLine();
if (infoFromClient.equals("bye")) {
System.out.println("对话结束");
s.close();
break;
}
System.out.println("客户端发来:" + infoFromClient);
//接收控制台输入的信息
System.out.println("输入你希望对客户端说的话:");
String response = br2.readLine();
//把从控制台接收的信息,回送给客户端
pw.println(response);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
</span>
2)全双工
TestClient3.java
package TestClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
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 TestClient3 extends JFrame implements ActionListener {
JTextArea jta = null;
JTextField jtf = null;
JButton jb =null;
JScrollPane jsp = null;
JPanel jpl = null;
PrintWriter pw = null;
public static void main(String[] args) {
TestClient3 client3 = new TestClient3();
}
public TestClient3() {
jta = new JTextArea();
jsp = new JScrollPane(jta);
jtf = new JTextField(20);
jb = new JButton("发送");
jb.addActionListener(this);
jpl = new JPanel();
jpl.add(jtf);
jpl.add(jb);
this.add(jsp, "Center");
this.add(jpl, "South");
this.setTitle("QQClient");
this.setSize(400, 300);
this.setVisible(true);
try {
Socket s = new Socket("127.0.0.1", 9988);
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(), true);
while(true){
//不断读取
String info = br.readLine();
jta.append("Server Say:" + info + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
//如果用户按下发送按钮
if(e.getSource() == jb){
//吧服务器在jtf写的内容发送给客户端
String info = jtf.getText();
//客户端发送的信息显示到jta中
jta.append("Client say: " + info + "\r\n");
pw.println(info);
jtf.setText("");
}
}
}
TestServer.java
package TestServer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
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 TestServer extends JFrame implements ActionListener {
JTextArea jta = null;
JTextField jtf = null;
JButton jb =null;
JScrollPane jsp = null;
JPanel jpl = null;
PrintWriter pw = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
TestServer server = new TestServer();
}
public TestServer(){
jta = new JTextArea();
jsp = new JScrollPane(jta);
jtf = new JTextField(20);
jb = new JButton("发送");
jb.addActionListener(this);
jpl = new JPanel();
jpl.add(jtf);
jpl.add(jb);
this.add(jsp, "Center");
this.add(jpl, "South");
this.setTitle("QQServer");
this.setSize(400, 300);
this.setVisible(true);
ServerSocket ss = null;
try {
//服务器监听
ss = new ServerSocket(9988);
//等待客户端连接
Socket s = ss.accept();
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(), true);
//读取客户端发来的信息
while (true) {
//读取客户端发来的信息
String info = br.readLine();
jta.append("Client say:" + info + "\r\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//如果用户按下发送按钮
if(e.getSource() == jb){
//吧服务器在jtf写的内容发送给客户端
String info = jtf.getText();
jta.append("Server say: " + info + "\r\n");
pw.println(info);
//清空内容
jtf.setText("");
}
}
}