C# Socket编程
来源:互联网 发布:如何检测网络丢包 编辑:程序博客网 时间:2024/06/13 22:27
服务端(server side)
sing System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading.Tasks;namespace MyChatServer{ internal class server { [STAThread] private static void Main(string[] args) { int recv; byte[] data = new byte[1024]; IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 9050); Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newSocket.Bind(ipEnd); newSocket.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newSocket.Accept(); IPEndPoint clientIp = client.RemoteEndPoint as IPEndPoint; Console.WriteLine("connect with client: " + clientIp.Address + " at port; " + clientIp.Port); string welcome = "welcome here"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None); while (true) { data = new byte[1024]; recv = client.Receive(data); Console.WriteLine("recv = " + recv); if (recv == 0) { break; } Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); client.Send(data, recv, SocketFlags.None); } Console.WriteLine("Disconnected from " + clientIp.Address); client.Close(); newSocket.Close(); } }}
客户端(Client Side)
using System;using System.Net;using System.Net.Sockets;using System.Text;namespace tcpclient{ /// <summary> /// Class1 的摘要说明。 /// </summary> class client { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // byte[] data=new byte[1024]; Socket newclient=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); Console.Write("please input the server ip:"); string ipadd=Console.ReadLine(); Console.WriteLine(); Console.Write("please input the server port:"); int port=Convert.ToInt32(Console.ReadLine()); IPEndPoint ie=new IPEndPoint(IPAddress.Parse(ipadd),port);//服务器的IP和端口 try { //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。 newclient.Connect(ie); } catch(SocketException e) { Console.WriteLine("unable to connect to server"); Console.WriteLine(e.ToString()); return; } int recv = newclient.Receive(data); string stringdata=Encoding.ASCII.GetString(data,0,recv); Console.WriteLine(stringdata); while(true) { string input=Console.ReadLine(); if(input=="exit") break; newclient.Send(Encoding.ASCII.GetBytes(input)); data=new byte[1024]; recv=newclient.Receive(data); stringdata=Encoding.ASCII.GetString(data,0,recv); Console.WriteLine(stringdata); } Console.WriteLine("disconnect from sercer"); newclient.Shutdown(SocketShutdown.Both); newclient.Close(); } }}
如果要在Unity中在web platform使用Socket编程(Window平台),需要做以下操作:
1、首先找到sockpol.exe文件。文件路径..\Unity\Editor\Data\Tools\SocketPolicyServer\sockpol.exe
2、然后打开cmd控制台,并且进入到sockpol.exe目录,打开sockpol服务,服务不能关闭。在cmd中使用netstat -ano命令可以看到一个843端口的服务。
3、在客户端连接(Unity中)的开始部分,加上下面这句代码。
Security.PrefetchSocketPolicy(ipAddress, 843); //端口号843固定,ipAddress为服务器的ip
0 0
- C# Socket 编程初探
- Socket基本编程--C#
- C# socket编程
- C#中的Socket编程
- C# Socket编程
- C# Socket编程
- C#网络编程(socket)
- C# Socket编程
- C# socket编程
- C# Socket编程
- C# Socket编程
- C# Socket编程笔记
- C# Socket编程
- C# Socket编程
- C# Socket编程
- C#异步Socket编程
- C# Socket编程笔记
- C# Socket编程浅谈
- s5pv210 android4.0.3调试,如何去掉SGX540硬件加速
- HDU 1261(全排列,大数乘除)
- occas resources security初探
- Hibernate学习笔记之EHCache的配置
- Android碎片Fragment使用简介
- C# Socket编程
- PCI的imagework已由freeview软件代替
- 寒假第三天--栈和队列--数据结构实验之栈四:括号匹配 (数组)
- Hibernate JPA 中配置Ehcache二级缓存
- POJ 2762 证明是否为单向连通图 强连通缩点+类拓扑排序
- Merge Two Sorted Lists
- 让 CentOS 启动时不启动桌面服务
- 贪心(二)
- 寒假第三天--栈和队列--数据结构实验之栈四:括号匹配 (栈)