Socket通信

来源:互联网 发布:中标数据查询360 编辑:程序博客网 时间:2024/05/17 12:55

服务端代码:

using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;namespace SocketServer{    class Server    {        //创建监听对象        private static TcpListener tcpServer = null;        public Server()        {            //获取本机ip地址            IPAddress iPAddress = IPAddress.Any;            //指定在哪个端口上监听            tcpServer = new TcpListener(iPAddress, 999);            //999是端口号,可以随便改 0-1024,主要不要和什么80,8080之类的常用端口号相冲突哦。              //开始监听            tcpServer.Start();            Console.WriteLine("监听已启动......");            //把收发放到后台线程运行            Thread t1 = new Thread(ExceMethod);            t1.IsBackground = true;            t1.Start();        }        private static void ExceMethod()        {            byte[] msg = Encoding.UTF8.GetBytes("服务端发出的数据");            while (true)            {                //接收数据的缓存                byte[] bytes = new byte[256];                //接收监听到的客户端                TcpClient client = tcpServer.AcceptTcpClient();                while (true)                {                    try                    {                        //把客户端的发送的数据存到缓冲区                         int i = client.Client.Receive(bytes);                        Console.WriteLine(DateTime.Now.ToString("G") + "接受:" + Encoding.UTF8.GetString(bytes));                        Console.WriteLine("\n");                        //发送数据给客户端                        client.Client.Send(msg);                    }                    catch                    {                        break;                    }                }                client.Close();                Thread.Sleep(1000);//10000单位是毫秒,系统在运行过程中,稍微有点停顿,个人感觉会更好一点。              }        }    }}

客户端代码:

using System;using System.Text;using System.Collections;using System.ComponentModel;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;using System.Collections.Generic;using System.Runtime.InteropServices;namespace SocketClient{    class Client    {        //创建客户端        private static TcpClient client = new TcpClient();        public Client()        {            //和服务器连接            client.Connect("127.0.0.1", 999);            Console.WriteLine("连接成功......");            //在线程收发数据            Thread t1 = new Thread(ExceMethod);            t1.IsBackground = true;            t1.Start();        }        private static void ExceMethod()        {            while (true)            {                byte[] data = Encoding.UTF8.GetBytes("客户端发出的数据");                //接收连接服务器的socket对象                Socket socket = client.Client;                //给服务器发送数据                socket.Send(data, data.Length, SocketFlags.None);                Console.WriteLine("发送成功:" + Encoding.UTF8.GetString(data));                  //接收服务器发送的数据                socket.Receive(data, SocketFlags.None);                Console.WriteLine("接受数据:" + Encoding.UTF8.GetString(data));                Thread.Sleep(1000);            }        }    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 屁股又宽又扁怎么办 烫伤一年多色差没有恢复怎么办 上古卷轴5不能动怎么办 上古卷轴ol卡死怎么办 小妹妹这么骚长大了怎么办 同事借钱忘了还怎么办 怀了二胎想离婚怎么办 如果闺蜜疏远了怎么办 考上博士但是硕士要延期怎么办 硕士延期也不能毕业怎么办 硕士论文工作量太少被延期了怎么办 竞彩足球比赛延期中断怎么办? 竞彩足球输了怎么办 讯飞语音不兼容百度怎么办 虎牙直播不兼容语音怎么办 为什么手机打开游戏就死机怎么办 2007cad打开时时死机怎么办 手机qq总是无响应怎么办 英雄联盟登录服务器未响应怎么办 苹果7p照相死机怎么办 小米手机qq打不开怎么办啊 电脑qq老是闪退怎么办 微信摄像卡住了怎么办 电脑打开应用程序没反应怎么办 宇飞来手机锁了怎么办 宇飞来手机忘记密码怎么办 vivo手机突然开不了机怎么办 苹果六关不了机怎么办 微信老是无响应怎么办 打游戏被骂了怎么办 微信运动跳转排行榜失败怎么办 小恩爱账号忘了怎么办 华为账号更换后游戏账号怎么办 注册游戏账号需要身份证怎么办 英雄联盟被裁决了怎么办 百度网盘密码重置不了怎么办 sap密码输入被锁怎么办 dnf二级密码错10怎么办 大网卡网速慢了怎么办 小米路由器无线速度慢怎么办 小米手机无线速度慢怎么办