找传奇、传世资源到传世资源站!

socket通讯实例(入门级)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

一个服务端监听程序源码,一个客户端发消息程序源码,入门级
from clipboard
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading.Tasks;namespace ConsoleApp3{ class Program { static void Main(string[] args) { int port = 6000; string host = "127.0.0.1";//服务器端ip地址 IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.Connect(ipe); //send message string sendStr = "send to server : hello,ni hao"; byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr); clientSocket.Send(sendBytes); //receive message string recStr = ""; byte[] recBytes = new byte[4096]; int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0); recStr = Encoding.ASCII.GetString(recBytes, 0, bytes); Console.WriteLine(recStr); clientSocket.Close(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复