using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { // 1,创建socket Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); //2,绑定ip跟端口号 222.20.30.68 IPAddress ipaddress = new IPAddress(new byte[]{222,20,30,68}); EndPoint point = new IPEndPoint(ipaddress,7788);//ipendpoint是对ip+端口做了一层封装的类 tcpServer.Bind(point);//向操作系统申请一个可用的ip跟端口号 用来做通信 //3,开始监听 (等待客户端连接) tcpServer.Listen(100);//参数是最大连接数 Console.WriteLine("开始监听"); Socket clientSocket = tcpServer.Accept();//暂停当前线程,直到有一个客户端连接过来,之后进行下面的代码 Console.WriteLine("一个客户端连接过来了"); //使用返回的socket跟客户端做通信 string message = "hello 欢迎你"; byte[] data = Encoding.UTF8.GetBytes(message);//对字符串做编码,得到一个字符串的字节数组 clientSocket.Send(data); Console.WriteLine("向客户端发送了一跳数据"); byte[] data2 = new byte[1024];//创建一个字节数组用来当做容器,去承接客户端发送过来的数据 int length = clientSocket.Receive(data2); string message2 = Encoding.UTF8.GetString(data2, 0, length);//把字节数据转化成 一个字符串 Console.WriteLine("接收到了一个从客户端发送过来的消息:"+message2); Console.ReadKey(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { //1,TcpListener对socket进行了一层封装,这个类里面自己会去创建socket对象 TcpListener listener = new TcpListener(IPAddress.Parse("222.20.30.68"), 7788); //2,开始进行监听 listener.Start(); //3,等待客户端连接过来 TcpClient client = listener.AcceptTcpClient(); //4,取得客户端发送过来的数据 NetworkStream stream = client.GetStream();//得到了一个网络流 从这个网络流可以取得客户端发送过来的数据 byte[] data = new byte[1024];//创建一个数据的容器,用来承接数据 while (true) { //0 表示从数组的哪个索引开始存放数据 //1024表示最大读取的字节数 int length = stream.Read(data, 0, 1024);//读取数据 string message = Encoding.UTF8.GetString(data, 0, length); Console.WriteLine("收到了消息:" + message); } stream.Close(); client.Close(); listener.Stop();//停止监听 Console.ReadKey(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; class Program { private static Socket udpServer; static void Main(string[] args) { //1,创建socket udpServer = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); //2,绑定ip跟端口号 udpServer.Bind( new IPEndPoint( IPAddress.Parse("192.168.0.112"),7788 ) ); //3,接收数据 new Thread(ReceiveMessage){ IsBackground = true}.Start(); //udpServer.Close(); Console.ReadKey(); } static void ReceiveMessage() { while (true) { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = new byte[1024]; int length = udpServer.ReceiveFrom(data, ref remoteEndPoint);//这个方法会把数据的来源(ip:port)放到第二个参数上 string message = Encoding.UTF8.GetString(data, 0, length); Console.WriteLine("从ip:" + (remoteEndPoint as IPEndPoint).Address.ToString() + ":" + (remoteEndPoint as IPEndPoint).Port + "收到了数据:" + message); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { //创建udpclient 绑定ip跟端口号 UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.0.112"),7788)); while (true) { //接收数据 IPEndPoint point = new IPEndPoint(IPAddress.Any, 0); byte[] data = udpClient.Receive(ref point);//通过point确定数据来自哪个ip的哪个端口号 返回值是一个字节数组,就是我们的数据 string message = Encoding.UTF8.GetString(data); Console.WriteLine("收到了消息:" + message); } udpClient.Close(); Console.ReadKey(); } }
using UnityEngine; using System.Collections; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; public class ChatManager : MonoBehaviour { public string ipaddress = "222.20.30.68"; public int port = 7788; public UIInput textInput; public UILabel chatLabel; private Socket clientSocket; private Thread t; private byte[] data = new byte[1024];//数据容器 private string message = "";//消息容器 // Use this for initialization void Start () { ConnectToServer(); } // Update is called once per frame void Update () { if (message != null && message != "") { chatLabel.text += "\n" + message; message = "";//清空消息 } } void ConnectToServer() { clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); //跟服务器端建立连接 clientSocket.Connect(new IPEndPoint(IPAddress.Parse("222.20.30.68"),7788)); //创建一个新的线程 用来接收消息 t = new Thread(ReceiveMessage); t.Start(); } /// <summary> /// 这个线程方法 用来循环接收消息 /// </summary> void ReceiveMessage() { while (true) { if (clientSocket.Connected == false) break; int length = clientSocket.Receive(data); message = Encoding.UTF8.GetString(data, 0, length); //chatLabel.text += "\n" + message; } } void SendMessage(string message) { byte[] data = Encoding.UTF8.GetBytes(message); clientSocket.Send(data); } public void OnSendButtonClick() { string value = textInput.value; SendMessage(value); textInput.value = ""; } void OnDestroy() { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close();//关闭连接 } }
using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace _022_聊天室_socket_tcp服务器端 { /// <summary> /// 用来跟客户端做通信 /// </summary> class Client { public string name=null; private Socket clientSocket; private Thread t; private byte[] data = new byte[1024];//这个是一个数据容器 public Client(Socket s,string i) { clientSocket = s; name = i; //启动一个线程 处理客户端的数据接收 t = new Thread(ReceiveMessage); t.Start(); } private void ReceiveMessage() { //一直接收客户端的数据 while (true) { //在接收数据之前 判断一下socket连接是否断开 if (clientSocket.Poll(10, SelectMode.SelectRead)) { clientSocket.Close(); break;//跳出循环 终止线程的执行 } int length = clientSocket.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); //接收到数据的时候 要把这个数据 分发到客户端 //广播这个消息 Program.BroadcastMessage(message,name); Console.WriteLine("收到 "+name +" 的消息:" + message); } } public void SendMessage(string message) { byte[] data = Encoding.UTF8.GetBytes(message); clientSocket.Send(data); } public bool Connected { get { return clientSocket.Connected; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Net.WebSockets; using System.Text; using System.Threading.Tasks; namespace _022_聊天室_socket_tcp服务器端 { class Program { static List<Client> clientList = new List<Client>(); /// <summary> /// 广播消息 /// </summary> /// <param name="message"></param> public static void BroadcastMessage(string message,string name) { var notConnectedList = new List<Client>(); foreach (var client in clientList) { if (client.Connected) client.SendMessage(name+" : "+message); else { notConnectedList.Add(client); } } foreach (var temp in notConnectedList) { clientList.Remove(temp); } } static void Main(string[] args) { int i = 0; string name = null; Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);//create tcpserver tcpServer.Bind(new IPEndPoint(IPAddress.Parse("222.20.30.68"),7788));//set ip and port tcpServer.Listen(100);//listen port Console.WriteLine("server running..."); while (true) { i = i + 1; if (i == 1) { name = "Tom"; } else if (i == 2) { name = "Alice";} else { name = "Other"; } Socket clientSocket = tcpServer.Accept();//accept client request Console.WriteLine("用户 "+name+" 已连接 !"); Client client = new Client(clientSocket,name);//把与每个客户端通信的逻辑(收发消息)放到client类里面进行处理 clientList.Add(client); } } } }
版权声明:本文为博主原创文章,未经博主允许,随便转载。
Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现
原文地址:http://blog.csdn.net/husheng0/article/details/48846425