Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <unistd.h>
- int main(int argc, char* argv[]){
- socklen_t sl;
- int sfd, cfd, on = 1;
- struct sockaddr_in saddr, caddr;
- saddr.sin_family = AF_INET;
- saddr.sin_addr.s_addr = INADDR_ANY;
- saddr.sin_port = htons(1234);
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &on, sizeof(on));
- bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
- listen(sfd, 5);
- char request[7];
- while(1){
- sl = sizeof(caddr);
- cfd = accept(sfd, (struct sockaddr*) &caddr, &sl);
- printf("%s -- new connection from %s:%d\n",
- argv[0],
- inet_ntoa(caddr.sin_addr),
- ntohs(caddr.sin_port));
- read(cfd, request, 7);
- if (strcmp(request, "132296") == 0)
- write(cfd, "Adrianna Nowicka\n", 17);
- else if (strcmp(request, "132237") == 0)
- write(cfd, "Wojciech Handkiewicz\n", 21);
- else
- write (cfd, "Error.\n", sizeof("Error.\n"));
- close(cfd);
- }
- close(sfd);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement