標籤:行資料 通過 str 編程 ble exception 建立 sse bsp
分為用戶端和服務端,分別進行收發操作
##########################################################################
用戶端:
###思路:
1、建立tcp用戶端服務
1.1因為是連線導向,必須有串連才有通訊
1.2在建立用戶端時,就必須明確目的地址和連接埠
2、一旦串連建立,就有了傳輸資料的通道。就可以在通道中進行資料轉送,這個傳輸是通過流實現的,是socket io流
3、擷取socket io中的寫動作就可以發送給服務端
###步驟:
1、建立socket對象,明確目的IP和連接埠
2、通過socket對象的getOutStream方法擷取OutputStream對象
3、通過OutputStream對象寫資料,實現發送
4、關閉socket對象
###代碼:System.out.println("client start");//1、建立用戶端對象,明確目的和連接埠Socket s = new Socket("192.168.10.141",10000);//2、擷取socket流中的輸出資料流,將資料發送給服務端OutputStream out = s.getOutputStream();//3、通過輸出資料流寫資料out.write("I miss you".getBytes());//4、關閉資源s.close();
####################################################################
服務端:
###思路:
1、建立socket伺服器端服務。伺服器監聽一個連接埠
2、擷取用戶端對象
3、擷取用戶端的socket流的讀取流
4、顯示
###步驟:
1、建立ServerSocket伺服器對象
2、通過ServerSocket對象的accept方法,擷取用戶端socket對象
3、通過用戶端socket對象的getInetAddress().getHostAddress() 方法擷取ip對象然後擷取ip
4、通過用戶端socket對象的getInputStream方法擷取InputStream對象
5、通過InputStream對象的read方法擷取用戶端發送的資料。
6、關閉用戶端socket對象。
###代碼:System.out.println("伺服器啟動。。。");//1、建立伺服器端對象ServerSocket ss = new ServerSocket(10003);//2、擷取用戶端對象Socket s = ss.accept();String ip = s.getInetAddress().getHostAddress();System.out.println(ip+"...connect");//3、通過用戶端對象擷取socket流的讀取流InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);String str = new String(buf,0,len);System.out.println(str);s.close();
################################################################################
簡單互動的TCP通訊
/*### * 案例二:實現用戶端和服務端的收發過程。 * 用戶端 */System.out.println("用戶端2 啟動.......");// 建立用戶端socket對象。明確服務端地址和連接埠。Socket s = new Socket("192.168.1.223", 10004);// 發送資料,通過socket輸出資料流完成。OutputStream out = s.getOutputStream();out.write("服務端,我來了".getBytes());// 讀取服務端返回的資料,通過socket輸入資料流InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);String text = new String(buf,0,len);System.out.println(text);// 關閉資源。s.close();
/*### * 案例二:實現用戶端和服務端的收發過程。 伺服器端。 */System.out.println("服務端2啟動.....");// 建立tcp服務端socket 明確連接埠。ServerSocket ss = new ServerSocket(10004);while (true) { // 擷取用戶端對象。 Socket s = ss.accept(); System.out.println(s.getInetAddress().getHostAddress() + ".....connected"); // 讀取用戶端的發送過來的資料 InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); String text = new String(buf, 0, len); System.out.println(text); // 給用戶端回饋資料。 OutputStream out = s.getOutputStream(); out.write("用戶端,我已到收到,哦耶!".getBytes()); // 關閉用戶端 s.close();}// 關閉服務端。如果不斷的擷取用戶端,不用關閉服務端。// ss.close();
###########################################################################
我想寫一個可以多次發送資訊的程式,沒有找到哪裡錯了
public class TCPServer2{ public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(10005); Socket s = ss.accept(); ServerReceive sr = new ServerReceive(s); ServerSend ssend = new ServerSend(s); new Thread(sr).start(); new Thread(ssend).start(); }}class ServerSend implements Runnable{ private Socket s; private OutputStream os; public ServerSend(Socket s) throws IOException { super(); this.s = s; os = s.getOutputStream(); } @Override public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; try { while((line = br.readLine()) != null){ os.write(line.getBytes()); if("//over".equals(line)){ break; } } s.close(); } catch (IOException e) { } }}class ServerReceive implements Runnable{ private Socket s; private InputStream is; public ServerReceive(Socket s) throws IOException { super(); this.s = s; is = s.getInputStream(); } @Override public void run() { while(true){ byte[] buf = new byte[1024]; try { int len = is.read(buf); String str = new String(buf,0,len); if("//over".equals(str)){ break; } System.out.println(str); s.close(); } catch (IOException e) { } } }}TCPServer
public class TCPClient2{ public static void main(String[] args) throws UnknownHostException, IOException { Socket bothSocket = new Socket("127.0.0.1",10005); Send2 send = new Send2(bothSocket); Receive2 receive = new Receive2(bothSocket); new Thread(send).start(); new Thread(receive).start(); }}//建立發送和接收的類,使用多線程,繼承runnable介面//多線程發送類class Send2 implements Runnable{ private Socket s; OutputStream os; public Send2(Socket s) throws IOException { super(); this.s = s; os = s.getOutputStream(); } @Override public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; try { while((line = br.readLine()) != null){ os.write(line.getBytes()); if("//over".equals(line)){ break; } } s.close(); } catch (IOException e) { } }}//多線程接收類class Receive2 implements Runnable{ private Socket s; private InputStream is; public Receive2(Socket s) throws IOException { super(); this.s = s; } @Override public void run() { while(true){ byte[] buf = new byte[1024]; try { is = s.getInputStream(); int len = is.read(buf); String str = new String(buf,0,len); if("//over".equals(str)){ break; } System.out.println(str); s.close(); } catch (IOException e) { } } }}TCPClient
026.3 網路編程 TCP聊天