一对一的基于TCP协议的Socket通信

服务端:
 
package com.kkoolerter;
import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintStream; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket;
public class TCPOne2OneServer {
    /** 
     * 一对一的基于TCP的服务器 
     * 
     * @param args 
     * @throws Exception 
     */ 
    public static void main(String[] args) throws Exception { 
        ServerSocket server = null; 
        Socket socket = null; 
        DataInputStream dis = null; 
        PrintStream out = null; 
        try { 
            // 设置侦听端口 
            server = new ServerSocket(12345); 
            // 获取Socket 
            socket = server.accept(); 
            // 获取输入流 
            dis = new DataInputStream(socket.getInputStream()); 
            // 获取输出流 
            out = new PrintStream(socket.getOutputStream()); 
            // 获取客户端的IP地址 
            InetAddress addr = server.getInetAddress(); 
            //发送欢迎信息 
            out.println("Welcome..."); 
            byte buffer [] = new byte[1024]; 
            //判断用户是否已经退出 
            int len = dis.read(buffer); 
            String msg = new String(buffer,len); 
            while(!msg.equals("quit")){ 
                System.out.println("from client:"+new String(msg)); 
                msg = dis.readLine(); 
            } 
            System.out.println("client leaved"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }finally{ 
            dis.close(); 
            out.close(); 
            socket.close(); 
            server.close(); 
            System.exit(0); 
        } 
    }
}
 
客户端:
package com.kkoolerter;
import java.io.DataInputStream; 
import java.io.OutputStream; 
import java.io.PrintStream; 
import java.net.Socket; 
import java.text.Format;
import javax.sound.sampled.AudioFormat.Encoding;
public class TCPOne2OneClient {
    /** 
     * @param args 
     */ 
    public static void main(String[] args) throws Exception { 
        Socket socket = null; 
        PrintStream ps = null; 
        DataInputStream dis = null;
        try { 
            socket = new Socket("127.0.0.1", 12345); 
            ps = new PrintStream(socket.getOutputStream()); 
            dis = new DataInputStream(socket.getInputStream()); 
            String msg = dis.readLine(); 
            System.out.println("from server:" + msg); 
            byte buffer[] = new byte[1024]; 
            System.in.read(buffer); 
            msg = new String(buffer, 0); 
            System.out.println(msg); 
            while (!msg.equals("quit")) { 
                ps.println(msg); 
                System.in.read(buffer); 
                msg = new String(buffer, "utf-8"); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            ps.close(); 
            dis.close(); 
            socket.close();
        } 
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值