Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Sockets/C/sockets.c
Normal file
37
Task/Sockets/C/sockets.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
const char *msg = "hello socket world";
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, sock, len, slen;
|
||||
|
||||
struct addrinfo hints, *addrs;
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if (0 == getaddrinfo("localhost", "256", &hints, &addrs))
|
||||
{
|
||||
sock = socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol);
|
||||
if ( sock >= 0 )
|
||||
{
|
||||
if ( connect(sock, addrs->ai_addr, addrs->ai_addrlen) >= 0 )
|
||||
{
|
||||
const char *pm = msg;
|
||||
do
|
||||
{
|
||||
len = strlen(pm);
|
||||
slen = send(sock, pm, len, 0);
|
||||
pm += slen;
|
||||
} while ((0 <= slen) && (slen < len));
|
||||
}
|
||||
close(sock);
|
||||
}
|
||||
freeaddrinfo(addrs);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue