大資料java篇——Socket程式設計
阿新 • • 發佈:2019-02-10
Socket:一個Socket由一個IP地址和一個埠號唯一確定
ServerSocket:伺服器端套接字,接收請求
TCP:傳輸控制協議,是可靠協議,必須先建立連線
UDP:使用者資料報協議
Socket
java在包java.net中提供了兩個類 Socket和ServerSocket,分別用來表示雙向連線的客戶端和服務端,其建構函式:
Socket(String host,int port);
Socket(String host,int port,boolean stream);
Socket(InetAddress address,int port);
Socket(InetAddress address,int port,boolean stream);
其中address,host和port分別是雙向連線中另一方的IP地址,主機名和埠號,stream指明socket是流socket還是資料報socket。
在選擇埠時,要選大於1023的數,因為0~1023是系統保留的埠號
package socketDemo; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketAddress; public class ServerSocketDemo { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(1234); while (true) { Socket socket = ss.accept(); InetAddress sa = socket.getInetAddress(); System.out.println("sa.getHostAddress()" + ":" + sa.getHostAddress()); System.out.println("sa.getHostName()" + ":" + sa.getHostName()); System.out.println("sa.getAddress()" + ":" + sa.getAddress()); System.out.println("socket.getPort()" + ":" + socket.getPort()); System.out.println(sa.toString() + "連線成功了"); System.out.println("正在接收"); } } catch (IOException e) { e.printStackTrace(); } } }
package socketDemo;
import java.net.Socket;
public class SocketDemo {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 1234);
System.out.println("socket");
} catch (Exception e) {
e.printStackTrace();
}
}
}
用Socket實現文字傳輸
package socketDemo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class ServerSocketDemo { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(1234); while (true) { Socket socket = ss.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is,"gbk")); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"gbk")); String line = null; while((line = br.readLine()) != null){ System.out.println("收到資料" + line); String send = "hello ," + line + "\r\n" ; bw.write(send); bw.flush(); System.out.println("傳送了" + send); } } } catch (IOException e) { e.printStackTrace(); } } }
package socketDemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class SocketDemo {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 1234);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"gbk"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"gbk"));
bw.write("abc\r\n");
bw.flush();
System.out.println(br.readLine());
bw.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}