0.
nc-lk Port number : always listen for data on this port on the local computer .
1. Import three header files
{
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
}
2. Socket writing steps
{
1. Create client socket socket (< #int #>, < #int #>, < #int #>); c8>
2. Create a server Socket struct sockaddr_in serveraddr ESS;
3. Connect to the server (Socket programming) Connect (< #int #>, < #const struct S Ockaddr *#>, < #socklen_t #>);
4. send data to server send (< #int #>, < #const void *#>, < #size_t #> < #int #>)
5. receive the data returned by the server recv (< #int #>, < #void *#>, < #size_t #> , < #int #>)
6. closing the Socket Close (socketnumber)
}
/*
Create a client Socket.
three parameters: domain: Network address types Type: Port type Protocal: Transport Protocol
Domain : the protocol field specifies the socket host address type . Network Layer Protocol Af_inet/ipv4 Protocol ; Af_inet_6/ipv6 Protocol
Type : Specifies the Socket Port type . Specify the Transport layer protocol type (TCP/UDP), Sock_stream (tcp/ stream ), Sock_dgram (udp/ message header )
protocal: Specify transport protocol : Common Protocol : Ipproto_tcp,ipproto_udp and so on, respectively, corresponding to TCP Transport protocol,UDP Transmission protocol .
The last parameter , 0, automatically selects the protocol corresponding to the second parameter according to the second parameter .
return value: If > 0 indicates success.
*/
// 0. Create client Socket.
int Socketnumber = socket (af_inet, sock_stream, 0);
if (Socketnumber > 0) {
NSLog (@ "Socket creation succeeded :%d", socketnumber);
}else{
NSLog (@ "Socket creation failed ");
};
/*
Connect to the server.
three parameters:
1. client socket.
2. the receiver's socket parameters .
3. Data length .
return value: 0 indicates success, other: error code .
*/
// 1. Server Socket
struct sockaddr_in serveraddress;
//IPV4 protocol.
serveraddress.sin_family = af_inet;
//Receiver (server) IP address.
serverAddress.sin_addr.s_addr = inet_addr ("127.0.0.1");
//port number.
serveraddress.sin_port = htons (56789);
// 2. Connect to server
// /serveraddress data length .
socklen_t length = sizeof(serveraddress);
//Connect to the server .
//In the C language, the length of the struct is specified when passing the structure body
//& takes the starting position of the data, only passing the length of one data, To ensure that the complete structure data is obtained .
//Return value: 0 success, others are failures.
int connection = connect (socketnumber, const struct SOCKADDR *) &serveraddress,length);
if (!connection) {
NSLog (@ " connection succeeded %d", connection);
}else{
NSLog (@ " Connection failed ");
}
/*
send a message to the server
parameters :
1> client Socket.
2> send content address .
3> send content length .
4> Send way identification , generally 0.
*/
// 3. send a message to the server
// Send message content
nsstring *msg = @ "Hello socket!";
msg.length: represents the length of the OC string .
Msg. Utf8string: Converts an OC string to an ASCII code of UTF8 , a Chinese character needs to occupy 3 the length of a byte .
strlen: calculates The length of all ASCII codes.
// Send message
ssize_t result = Send (Socketnumber, MSG. Utf8string, strlen (Msg. utf8string), 0);
NSLog (@ "result =%ld", result);
/*
receiving the message returned by the server
parameters :
1> client Socket.
2> receive content buffers .
3> receives the content buffer length .
4> receive mode . 0 indicates a blocking type . You must wait for the server to return data .
return value:
If successful, returns the number of bytes received. failure is returned Socket_error
*/
// 4. server receives messages
// Create receive content buffers .
uint8_t buffer[];
// Accept message
ssize_t len = recv (socketnumber, buffer, sizeof(buffer), 0);
NSLog (@ "len:%zd", Len);
// Remove data from the accepted content buffer .
NSData *data = [NSData datawithbytes:buffer Length:len];
//Convert binary stream data to string type.
nsstring *receive = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "receive:%@", receive);
// 5. Close Socket
Close (socketnumber);
Socket Introduction (Master Socket family)