Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct sockaddr_storage their_addr;
- socklen_t addr_size;
- struct addrinfo hints, *servinfo;
- int sockfd, new_fd ,bindfd, listenfd,connectfd;
- char recvmessage[500];
- int len, bytes_sent,bytes_received;
- char MYPORT[5] = "8080";
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
- fprintf(stderr, "WSAStartup failed.\n");
- exit(1);
- }
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
- hints.ai_socktype = SOCK_STREAM; //TCP
- hints.ai_flags = AI_PASSIVE; // fill in my IP for me
- //Info
- getaddrinfo(NULL,MYPORT, &hints, &servinfo);
- //Create a socket
- sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
- if(sockfd > 0)
- printf("\n\Socket(%i) OK\n\n",sockfd);
- else
- wprintf(L"### Bind failed with error: %ld\n\n", WSAGetLastError());
- //Bind
- bindfd = bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
- if(bindfd == 0)
- printf("Bind OK\n\n",bindfd);
- else
- wprintf(L"### Bind failed with error: %ld\n\n", WSAGetLastError());
- //Listen
- listenfd = listen(sockfd, 10);
- if(listenfd == 0)
- printf("Listening on port %s...\n\n",MYPORT);
- else
- wprintf(L"### Listen failed with error: %ld\n\n", WSAGetLastError());
- //Accept
- addr_size = sizeof their_addr;
- new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
- if (new_fd > 0)
- printf("Accepting %i bytes\n\n",new_fd);
- else if (new_fd == -1)
- wprintf(L"### Accept failed with error: %ld\n\n", WSAGetLastError());
Add Comment
Please, Sign In to add comment