简单Socket编程,来理解Socket

Server端:

java 代码
  1. package com.sunhill.socket.server;   
  2.   
  3. import java.io.*;   
  4. import java.net.*;   
  5.   
  6. public class tcpserver {   
  7.     public static void main(String[] args) throws IOException {   
  8.         ServerSocket svrsoc = null;   
  9.         Socket soc = null;   
  10.         DataInputStream in = null;   
  11.         PrintStream out = null;   
  12.         InetAddress clientIP = null;   
  13.         String str = null;   
  14.         try {   
  15.             svrsoc = new ServerSocket(8000);   
  16.             System.out.println("Server start....");   
  17.             soc = svrsoc.accept();   
  18.   
  19.             in = new DataInputStream(soc.getInputStream());   
  20.             out = new PrintStream(soc.getOutputStream());   
  21.             clientIP = soc.getInetAddress();   
  22.             System.out.println("Client's IP address:" + clientIP);   
  23.             out.println("welcome.....");   
  24.             str = in.readLine();   
  25.             while (!str.equals("quit")) {   
  26.                 System.out.println("Client said:" + str);   
  27.                 str = in.readLine();   
  28.             }   
  29.             System.out.println("Client want to leave");   
  30.         } catch (Exception e) {   
  31.             System.out.println("error:" + e);   
  32.         } finally {   
  33.             in.close();   
  34.             out.close();   
  35.             soc.close();   
  36.             svrsoc.close();   
  37.             System.exit(0);   
  38.         }   
  39.     }   
  40. }   

Client端:

java 代码
  1. package com.sunhill.socket.client;   
  2.   
  3. import java.io.*;   
  4. import java.net.*;   
  5.   
  6. public class tcpclient {   
  7.     public static void main(String[] args) throws IOException {   
  8.         Socket soc = null;   
  9.         DataInputStream in = null;   
  10.         PrintStream out = null;   
  11.         DataInputStream sysin = null;   
  12.         String strin = null;   
  13.         String strout = null;   
  14.         try {   
  15.             soc = new Socket("localhost"8000);   
  16.             System.out.println("Connecting to the Server");   
  17.             in = new DataInputStream(soc.getInputStream());   
  18.             out = new PrintStream(soc.getOutputStream());   
  19.             strin = in.readLine();   
  20.             System.out.println("Server said:" + strin);   
  21.             sysin = new DataInputStream(System.in);   
  22.             strout = sysin.readLine();   
  23.             while (!strout.equals("quit")) {   
  24.                 out.println(strout);   
  25.                 strout = sysin.readLine();   
  26.             }   
  27.             out.println(strout);   
  28.         } catch (Exception e) {   
  29.             System.out.println("error:" + e);   
  30.         } finally {   
  31.             in.close();   
  32.             out.close();   
  33.             soc.close();   
  34.             sysin.close();   
  35.             System.exit(0);   
  36.         }   
  37.     }   
  38. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值