Unity3d realization of public chat room based on socket communication

Source: Internet
Author: User

Using multiple clients together is a simple public chat room. Server for a console program using C # Implementation, of course, in the Unity3d also use the C # language to implement the client, server and client can realize the exchange of messages, when the server received a message sent by a client will be the client List members broadcast, This is the most basic form of a public chat room. Socket communication is the most basic knowledge of online games, so this example can provide guiding significance for beginners who are interested in the online game industry.

Using System;  
Using System.Collections.Generic;  
Using System.Linq;  
Using System.Text;  
Using System.Net.Sockets;  
Namespace TestServer  
{  
class program  
{  
//Set connection port  
const int PORTNO =;  
static void Main (string[] args)  
{  
//Initialize server IP  
System.Net.IPAddress localadd = System.Net.IPAddress.Parse ("127.0.0.1");  
Create a TCP listener  
tcplistener listener = new TcpListener (Localadd, portno);  
Listener. Start ();  
Displays server startup information  
Console.WriteLine ("Server is starting...\n");  
Loops accept the client's connection request while  
(true)  
{  
chatclient user = new ChatClient (listener). AcceptTcpClient ());  
Displays the IP and Port  
Console.WriteLine (User._clientip + "is joined...\n") of the Connection Client;}}}  

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.