ios-Network Socket operation

Source: Internet
Author: User

Socket
The socket is also called a socket, and the application sends a request to the network or responds to the network through sockets
Network communication is actually the communication between sockets
Data transfers data between two sockets via IO
The socket is a pure C language and is a cross-platform
The HTTP protocol is socket-based, and the HTTP protocol is used at the bottom of the socket

The communication process of the socket
Create socket
Connect to Server
Sending data to the server
Receiving data from the server
Close connection

Creation of sockets
Import Header File
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
Create socket
int socket (int domain, int type, int protocol);

Parameter 1 Domain protocol domains Af_inet--ipv4
Parameter 2 type SOCKET sock_stream (TCP)/socket_dgram (UDP)
Parameter 3 protocol IPPROTO_TCP/IPPROTO_UDP if incoming 0 selects the appropriate value according to the second parameter

Return value >0 descriptor for creating a successful socket

Example: int clientsocket = socket (af_inet, sock_stream, ipproto_tcp);

Connecting to a server

Open Netcat, Simulate server
Open Terminal
Nc-lk 12345
Tools for debugging and checking the network under the Nc-->netcat terminal
Connecting to a server
int connect (int sockfd,struct sockaddr * serv_addr,int addrlen);
Parameter 1 SOCKFD Client socket
Parameter 2 serv_addr server address struct-body pointer
Parameter 3 addrlen structure data length
Return value succeeds returns 0, failure returns non 0

Example:
Return value succeeds returns 0, failure returns non 0
struct sockaddr_in serveraddr;
IPV4
serveraddr.sin_family = af_inet;
Port
Serveraddr.sin_port = htons (12345);
IP Address
SERVERADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");

int connresult = connect (clientsocket, (const struct sockaddr *) &serveraddr, sizeof (SERVERADDR));
if (Connresult = = 0) {
NSLog (@ "connected successfully");
}else{
NSLog (@ "Connection failed");
}

Socket to send and receive data

Sending data to the server
const char *sendmessage = "Hello World";
ssize_t Sendlen = Send (Clientsocket, SendMessage, strlen (SendMessage), 0);
NSLog (@ "number of bytes Sent:%ld", Sendlen);
Data returned by the receiving server
uint8_t buffer[1024];
ssize_t Recvlen = recv (clientsocket, buffer, sizeof (buffer), 0);
if (Recvlen > 0) {
NSLog (@ "received%ld bytes", Recvlen);
Reading data from a buffer
NSData *data = [NSData datawithbytes:buffer Length:recvlen];
NSString *msg = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "%@", msg);
}
Close the connection close (Clientsocket);

ios-Network Socket operation

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.