Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #define MAX 80
- #define PORT 8080
- #define SA struct sockaddr
- int func(int sockfd){
- char recvBuff[20];
- int n;
- write(sockfd,"Hello, my name is lambda. Who are you?\n",39);
- //FILE * f = fopen("log3.txt","a+");
- /*
- n = recv(sockfd, recvBuff, 20,0);
- if (n<0)
- return 0;
- //printf("%d\n",strcmp(recvBuff,"ha"));
- FILE * f = fopen("log3.txt","a+");
- fputs(recvBuff,f);
- fclose(f);
- if (strcmp(recvBuff,"ha\n")!=0){
- write(sockfd,"Oh, sorry, no...\n",17);
- return 0;
- }
- write(sockfd,"Ready\n",6);*/
- n = recv(sockfd, recvBuff, 200,0);
- //fputs(recvBuff,f);
- //fclose(f);
- printf("%d\n",n);
- }
- int main()
- {
- int sockfd, connfd, len;
- struct sockaddr_in servaddr, cli;
- // socket create and verification
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
- if (sockfd == -1) {
- printf("socket creation failed...\n");
- exit(0);
- }
- else
- printf("Socket successfully created..\n");
- bzero(&servaddr, sizeof(servaddr));
- // assign IP, PORT
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(31337);
- // Binding newly created socket to given IP and verification
- if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
- printf("socket bind failed...\n");
- exit(0);
- }
- else
- printf("Socket successfully binded..\n");
- // Now server is ready to listen and verification
- if ((listen(sockfd, 5)) != 0) {
- printf("Listen failed...\n");
- exit(0);
- }
- else
- printf("Server listening..\n");
- len = sizeof(cli);
- // Accept the data packet from client and verification
- while (1){
- connfd = accept(sockfd, (SA*)&cli, &len);
- if (connfd < 0) {
- printf("server acccept failed...\n");
- exit(0);
- }
- else
- printf("server acccept the client...\n");
- if (fork() ==0){
- func(connfd);
- close(connfd);
- }
- }
- close(sockfd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement