(同步)Socket服务端和客户端

服务器同步套接字编程套路:

1.IP;

IPAddress ipAddress = IPAddress.Parse("192.168.0.1");

2.端口;

IPEndPoint ipEndPoint = new IPEndPoint(ipAddress,7777);

3.创建监听套接字;

Socket listenSocket = new Socket(AddressFamily.InterNetWork,SocketType.Stream,ProtocolType.Tcp);

4.把套接字绑定到IP和端口上;

listenSocket.Bind(ipEndPoint);

5.启动监听;

listenSocket.Listen(5);//监听个数

print("已经开始监听了,等待连接进来");

6.承认连接:创建一个和客户端连接的套接字;

Socket connClientSocket = listenSocket.Accept();//阻塞

print("已经和" + connClientSocket.RemoteEndPoint + "连接上了");

7.通过和客户端连接的套接字进行发送和接收;

8.停止一切发送和接收,关闭客户端套接字;

connClientScoket.Shutdown(SocketShutdown.Both);

connClientSocket.Close();

9.关闭监听套接字

listenSocket.Close();

同步服务器端

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System;
public class newSocket : MonoBehaviour {
    void Start () {
        Socket listenSocket = null;
        Socket connClientSocket = null;
        try
        {
            //创建一个IP
            IPAddress ipAddress = IPAddress.Parse("192.168.0.105");
            //创建一个终结点(端口)
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress,7777);
            //创建一个用于监听的套接字
            listenSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //绑定监听套接字到IP和端口上
            listenSocket.Bind(ipEndPoint);
            //开始监听
            listenSocket.Listen(2);
            print ("已经开始监听了,等待连接进来!");
            //承认连接
            connClientSocket = listenSocket.Accept();//阻塞
            print("已经和" + connClientSocket.RemoteEndPoint + "连接上了");
            //发送信息给客户端(//using System.Text;)
            string str = connClientSocket.RemoteEndPoint + ":你好";
            connClientSocket.Send (Encoding.UTF8.GetBytes (str));
            print ("已经发送");
            //接受客户端发来的信息
            byte[] buf = new byte[1024];
            print ("将要接收");
            int len = connClientSocket.Receive(buf);//阻塞
            print("已经接收");
            //把接受的信息打印在控制台
            print(Encoding.UTF8.GetString(buf,0,len));
        }
        catch(Exception e) 
        {
            print (e.Message);
        }
        finally 
        {
            //如果已经创建了客户端的连接
            if (connClientSocket != null)
            {
                //关闭客户端的发和收的操作
                connClientSocket.Shutdown (SocketShutdown.Both);
                //关闭和客户端的连接
                connClientSocket.Close ();
            }
            if (listenSocket != null
            {
                //关闭监听套接字
                listenSocket.Close ();
            }
        }
    }
    void Update () {
    }
}

同步客户端

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System;
public class oldSocket : MonoBehaviour {
    void Start () {
        Socket connClientSocket = null;
        try
        {
            //你要连接服务器的IP
            IPAddress ipAddress = IPAddress.Parse("192.168.0.105");
            //服务器的IP&Port(IP和端口)
            IPEndPoint ipEndPoit = new IPEndPoint(ipAddress,7777);
            //连接服务器的套接字
            connClientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            //连接服务器
            connClientSocket.Connect(ipEndPoit);
            print ("连接服务器成功!");
            //接收
            byte[] buf = new byte[1024];
            int len = connClientSocket.Receive (buf);
            print (Encoding.UTF8.GetString(buf,0,len));
            //发送
            string str = "你好";
            connClientSocket.Send (Encoding.UTF8.GetBytes(str));
            print ("发送成功");
        }
        catch(Exception e) 
        {
            print (e.Message);
        }
        finally 
        {
            if (connClientSocket != null)
            {
                //关闭操作
                connClientSocket.Shutdown (SocketShutdown.Both);
                //关闭套接字
                connClientSocket.Close();
            }
        }
    }
    void Update () {
    }
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值