Sockct:插座
Socket是关于TCP的。
端口号:两个字节->65536个端口号,一个应用程序占多个端口号;
但是假设一个应用程序占一个端口号;一台电脑会有65535个应用程序。
自己编写程序要占用端口号1024以上后的。
80端口:网页
端口号分为:TCP端口和UDP端口。每个有65535个端口。
通过管道(流)来说话
栗子:服务器端:
package com.zhj.www;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.*;
public class TCPServer {public static void main(String[] args)throws Exception {ServerSocket ss = new ServerSocket(6666);//服务器监听6666端口口while(true) { Socket s =ss.accept();//自身建立一个Socket与客户端连接System.out.println("A client connect!");//阻塞式的连接,一个客户端连接,另外一个客户端不会链接。DataInputStream dis = new DataInputStream(s.getInputStream());System.out.println(dis.readUTF());dis.close();s.close();}}
}
客户端:
package com.zhj.www;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.*;
public class TCPClient {public static void main(String[] args) throws Exception {Socket s = new Socket("127.0.0.1", 6666);//建立连接,client随机某个端口去连接服务器的6666OutputStream oStream = s.getOutputStream();//拿到输出管道;DataOutputStream dataOutputStream = new DataOutputStream(oStream);dataOutputStream.writeUTF("hello server!");dataOutputStream.flush();dataOutputStream.close();s.close();}
}
-----------------------------------------------------------------------------
---------------------------------------------------------------------------->
栗子1:(一端说,一端听):TestServer.java &&TestClient.java
服务器端:package com.zhj.www;import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;public class TestServer {/*拿到服务器端的数据写到客户端*/public static void main(String[] args) {try {ServerSocket ss = new ServerSocket(8888);while(true) {Socket socket = ss.accept();OutputStream oStream = socket.getOutputStream();DataOutputStream dataOutputStream = new DataOutputStream(oStream);dataOutputStream.writeUTF("Hello,"+socket.getInetAddress()+"port #"+socket.getPort()+" bye-bye");//客户端的端口号是随机的dataOutputStream.close();socket.close();}}catch (Exception e) {e.getStackTrace();System.out.println("程序运行出错:"+e);}}}
客户端:
package com.zhj.www;import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.Socket;public class TestClient {public static void main(String[] args) {try {Socket socket = new Socket("127.0.0.1", 8888);InputStream is =socket.getInputStream();DataInputStream dInputStream = new DataInputStream(is);System.out.print(dInputStream.readUTF());dInputStream.close();socket.close();}catch (ConnectException connExc) {connExc.getStackTrace();System.out.println("服务器连接失败!");} catch (IOException e) {e.getStackTrace();}}}
--------------------------------------------------------------------------------->
栗子2(两端都说,两端都听) TestSocketServer.java && TestSocketClient.java
服务器端:
package com.zhj.www;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;public class TestSocketServer {public static void main(String[] args) {InputStream in = null;OutputStream out = null;try {ServerSocket ss = new ServerSocket(5888);Socket socket = ss.accept();in = socket.getInputStream();out = socket.getOutputStream();DataOutputStream dataOutputStream = new DataOutputStream(out);DataInputStream dataInputStream = new DataInputStream(in);String string = null;if ((string = dataInputStream.readUTF()) != null) {System.out.println(string);System.out.println("from: "+socket.getInetAddress());System.out.println("Port:"+socket.getPort());}dataOutputStream.writeUTF("hi,hello");dataOutputStream.close();dataInputStream.close();socket.close();} catch (IOException e) {e.printStackTrace();}}
}
客户端:
package com.zhj.www;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;public class TestSocketClient {public static void main(String[] args) {InputStream inputStream =null;OutputStream outputStream = null;try {Socket socket= new Socket("localhost", 5888);inputStream = socket.getInputStream();outputStream =socket.getOutputStream();DataInputStream dataInputStream = new DataInputStream(inputStream);DataOutputStream dataOutputStream = new DataOutputStream(outputStream);dataOutputStream.writeUTF("hey");String string = null;while((string = dataInputStream.readUTF())!= null) {System.out.println(string); }dataOutputStream.close();dataInputStream.close();socket.close();}catch (UnknownError e) {e.printStackTrace();} catch (IOException e) {e.getStackTrace();}}
}
栗子3:聊天程序: