Last active
March 23, 2022 19:07
-
-
Save Silva97/26539d7bc8812f68eded47017a9f6043 to your computer and use it in GitHub Desktop.
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
/******************** | |
* Exemplo básico de uso de sockets no Linux. | |
* Por Luiz Felipe - https://github.com/Silva97 | |
* | |
* Link do vídeo: https://youtu.be/GaxjJvMnz-I | |
********************/ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
int main(){ | |
char buff[129]; | |
struct sockaddr_in caddr; | |
struct sockaddr_in saddr = { | |
.sin_family = AF_INET, | |
.sin_addr.s_addr = htonl(INADDR_ANY), | |
.sin_port = htons(5000) | |
}; | |
int server = socket(AF_INET, SOCK_STREAM, 0); | |
int client, x; | |
int csize = sizeof caddr; | |
bind(server, (struct sockaddr *) &saddr, sizeof saddr); | |
listen(server, 5); | |
while(1){ | |
client = accept(server, (struct sockaddr *) &caddr, &csize); | |
x = recv(client, buff, sizeof buff, 0); | |
send(client, buff, x, 0); | |
puts(buff); | |
fflush(stdout); | |
close(client); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment