Socket编程:(一般的网络编程)
<1> 两个 JAVA 应用程序可通过一个双向的网络通信连接, 实现数据交换, 这个双向链路的一段称为一个 Socket.
<2> Socket 通常用来实现 Client-Server 连接.
<3> java.net 包中定义的两个类 Socket 和 ServerSocket(阻塞式), 分别用来实现双向连接的 Client 和 Server 端.
<4> 建立连接时, 所需的寻址信息为远程计算机的 IP 地址和端口号(Port number, 2个字节, 65536个端口, 一个应用程序可能有多个端口号)
TCP端口、UDP端口分开的, 在技术上来讲, 只有TCP才分 Server 、Client.
每一个有65536个端口, 端口号用来区分同一台机器上的不同应用程序, 一个人占用了某个端口号, 其他人就不能占了.
自己编写程序的话, 占1024以后的端口号, 1024以前的不要占, 系统可能会随时征用.
TCP、UDP二者是不一样的.
<5> 默认的网页 http 端口为 80端口;
SMTP(邮件发送):25端口
POP3(接收邮件):110端口
<6> 写 TCPserver、TCPclient 程序时, 首先启动 Server, 再启动 Client, 同时写程序, 一起开发.
Demo_1:
Server端:
import java.io.*; import java.net.*; public class TCPserver { public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(6666); while(true){ Socket s = ss.accept(); System.out.println("A client connect: "); DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readUTF()); //阻塞式(readUTF), 死死地等待 dis.close(); s.close(); } } }
Client端:
import java.net.*; import java.io.*; public class TCPclient { public static void main(String[] args) throws Exception{ Socket s = new Socket("192.168.56.1", 6666); OutputStream os = s.getOutputStream(); //拿到输出的那根管道, 往里面写 DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("Hello Server"); dos.flush(); dos.close(); s.close(); } }
运行结果:
A client connect:
Hello Server
Demo_2:
Server端:
import java.net.*; import java.io.*; public class TestServer { public static void main(String[] args) { try { Socket s1 = new Socket("192.168.56.1", 8888); InputStream is = s1.getInputStream(); DataInputStream dis = new DataInputStream(is); System.out.println(dis.readUTF()); dis.close(); s1.close(); } catch (UnknownHostException e) { e.printStackTrace(); System.out.println("错误端口号, 请核对------"); } catch (IOException e) { e.printStackTrace(); } } }
Client端:
import java.net.*; import java.io.*; public class TestClient { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); while(true){ Socket s1 = ss.accept(); OutputStream os = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("Hello, "+s1.getInetAddress()+"Port#, "+s1.getPort()+"Bye--Bye"); dos.close(); os.close(); } } catch (IOException e) { e.printStackTrace(); System.out.println("程序出错啦:"+e); } } }
运行结果:Hello, /192.168.56.1Port#, 4391Bye--Bye
Demo_3:
import java.net.*; import java.io.*; public class TestSockServer { public static void main(String[] args) { InputStream in = null; OutputStream os = null; try { ServerSocket ss = new ServerSocket(5888); Socket socket = ss.accept(); in = socket.getInputStream(); os = socket.getOutputStream(); DataInputStream dis = new DataInputStream(in); DataOutputStream dos = new DataOutputStream(os); String s = null; if((s=dis.readUTF())!=null){ System.out.println(s); System.out.println("From: "+socket.getInetAddress()); System.out.println("Port: "+socket.getPort()); } } catch (IOException e) { e.printStackTrace(); } } }
Client端:
import java.net.*; import java.io.*; public class TestSockClient { public static void main(String[] args) { InputStream is = null; OutputStream os = null; try { Socket socket = new Socket("192.168.56.1", 5888); is = socket.getInputStream(); os = socket.getOutputStream(); DataInputStream dis = new DataInputStream(is); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("Hey"); String s = null; if((s = dis.readUTF())!=null){ System.out.println(s); } dos.close(); dis.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
运行结果:
Hey
From: /192.168.56.1
Port: 4531
Demo_4:
Server 端:
import java.net.*; import java.io.*; public class TalkServer { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(4702); Socket socket = server.accept(); BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(socket.getOutputStream()); System.out.println("Client: "+is.readLine()); String line = null; line = sin.readLine(); while(!line.equalsIgnoreCase("Bye")){ pw.println(line); pw.flush(); System.out.println("Client: "+is.readLine()); line = sin.readLine(); } is.close(); pw.close(); socket.close(); server.close(); } catch (IOException e) { e.printStackTrace(); } } }
Client 端:
import java.net.*; import java.io.*; public class TalkClient { public static void main(String[] args) { try { Socket socket = new Socket("192.168.56.1", 4702); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter os = new PrintWriter(socket.getOutputStream()); String readLine = null; readLine = sin.readLine(); while(!readLine.equalsIgnoreCase("Bye")){ os.println(readLine); os.flush(); System.out.println("Servber: "+is.readLine()); readLine = sin.readLine(); } os.close(); is.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
运行结果展示: