Unity learning socket (1)

Source: Internet
Author: User

I wrote a blog for writing. No matter the quality, I had to clean it up.

I feel so sad to myself. I have never done online games before I went out for an internship, and I almost didn't use socket. So now I have nothing to do at school. I have to learn more about network communication and so on. Let's start from scratch. The first example below is very simple. Don't laugh at me. In this example, we use the TCP protocol of socket. Of course we can also use UDP and tcplistener. I didn't use multithreading. Oh, it's just to look at some functions in it.

Server. CS:

Using unityengine; using system. collections; using system. net; using system. io; using system. net. sockets; using system. text; public class server: monobehaviour {void start () {openserver ();} void openserver () {IPaddress ipadr = IPaddress. parse ("10.56.03.32"); ipendpoint ipep = new ipendpoint (ipadr, 1234); socket serverscoket = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); serve Rscoket. BIND (ipep); serverscoket. listen (20); While (true) {Socket Client = serverscoket. accept (); byte [] request = new byte [512]; int bytesread = client. receive (request); string input = encoding. utf8.getstring (request, 0, bytesread); print ("Server Request:" + input); string output = "successfully connected to the server ~~~~ "; Byte [] concent = encoding. utf8.getbytes (output); client. Send (concent); client. Shutdown (socketshutdown. Both); client. Close ();}}}

 

 

Client. CS:

Using unityengine; using system. collections; using system. text; using system. net; using system. net. sockets; using system. io; public class client: monobehaviour {void start () {conncetserver ();} void conncetserver () {IPaddress ipadr = IPaddress. parse ("10.56.03.32"); ipendpoint ipep = new ipendpoint (ipadr, 1234); socket clientscoket = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP) ; Clientscoket. Connect (ipep); string output = "client request connection ~~~ "; Byte [] concent = encoding. utf8.getbytes (output); clientscoket. send (concent); byte [] response = new byte [1024]; int bytesread = clientscoket. receive (response); string input = encoding. utf8.getstring (response, 0, bytesread); print ("client request:" + input); clientscoket. shutdown (socketshutdown. both); clientscoket. close ();}}

Server:

1). Use SOCKET () to obtain a socket description

2). BIND () J to bind the socket to a network address (generally the local IP address)

3) Use listen () to start listening on a port

4). Accept () waits for the client to connect. If the client calls the connect () function to connect to the server, accept () will obtain the client (socket ).

5). Receive () receives data

6). Send () to send data

 

 

Client:

1). Use SOCKET () to obtain a socket description

2) connect () to a remote host

3). Send () to send data

4). Receive () receives data

 

 

 

 

 

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.