Scoket:1.socket
/**
First parameter (domain): Indicates what protocol af_inet developed for IPV4
The second parameter (type): Indicates scoket why the type Sock_stream is TCP Sock_dgram (UDP, packet);
Third parameter (protocol): If output 0 represents the type of scoket to set the corresponding protocol
If the return value >0 indicates success
*/
Self.clientsocket=socket (af_inet, sock_stream, 0);
2.connect
/**
Parameters
1> Client Socket
2> pointer to the data structure sockaddr, which includes the destination port and IP address
The "struct" address of the server
Hint: There are no objects in C language
3> Structure Data length
return value
0 Success/Other error code, not 0 is true
*/
struct sockaddr_in serveraddress;
1> Protocol Family
serveraddress.sin_family = af_inet;
2> IP looking for machine inet_addr will do byte flipping of address
SERVERADDRESS.SIN_ADDR.S_ADDR = inet_addr (host. utf8string); Host is the string to send
3> Port Find program, swap the high and low bits of integers (byte flipping)
Serveraddress.sin_port = htons (port);//port is port;
Connect (self.clientsocket, (const struct sockaddr *) &serveraddress, sizeof (serveraddress));
Send
/**
Parameters
1> Client Socket
2> Send content address void * = = ID
3> Send Content length = bytes length
4> send way flag, typically 0
return value
If successful, returns the number of bytes sent, and the failure returns SOCKET_ERROR
*/
ssize_t Sendlen = Send (Self.clientsocket, MSG. Utf8string, strlen (Msg. utf8string), 0);
NSLog (@ "sent%ld%tu%ld", Sendlen, Msg.length, strlen (msg). utf8string));
Receive data
/**
Parameters
1> socket
2> address to receive content
3> length
4> receive flag, if it is 0, marked blocking, waiting for the server to return data
In the C language, the name of the array is the pointer to the first element of the array
return value
The length of the received data
*/
uint8_t buffer[1024];
ssize_t Recvlen = recv (self.clientsocket, buffer, sizeof (buffer), 0);
NSLog (@ "Receive%ld bytes", Recvlen);
Gets binary data returned by the server
NSData *data = [NSData datawithbytes:buffer Length:recvlen];
Convert to String
NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
Disconnect Connection
Disconnect Connection
Close (Self.clientsocket);
}
Note: Basically these 5 parts can reach the data transmission and reading, the test need to enter the command in the terminal. Sorry instruction forgot ... Tomorrow night to make up ...
Web Note 01-2 scoket