Last active
October 17, 2017 00:36
-
-
Save rozanecm/33edf48f7964b969e5b6fc14a235e74b to your computer and use it in GitHub Desktop.
Basic client socket in C, wo error check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUF_SIZE | |
#define IP | |
#define PORT | |
int main(int argc, char* argv[]){ | |
structo addrinfo hints; | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
hints.ai_family = AF_INET; //case for IPv4. Use AF_INET6 for IPv6 or AF_UNSPEC for either | |
hints.ai_socktype = SOCK_STREAM; | |
struct addrinfo *result; | |
getaddrinfo(IP, PORT, &hints, &result); | |
int sfd = socket(&result->family, &result->socktype, &result->protocol); | |
connect(sfd, &result->addr, &result->addrlen); | |
freeaddrinfo(&result); | |
char buf[BUS_SIZE] | |
int bytes_recv = 0 | |
while (MSG_LEN > bytes_recv && skt_still_open) { | |
bytes_recv += recv(sfd, &buf[bytes_recv], MSG_LEN - bytes_recv - 1, MSG_NOSIGNAL); | |
} | |
/* idem para send */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment