Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <sys/types.h>
- #include <winsock.h>
- #include <iostream>
- #include <sstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- using namespace std;
- #define MYPORT "3490" // the port users will be connecting to
- #define BACKLOG 10 // how many pending connections queue will hold
- void *get_in_addr(struct sockaddr *sa)
- {
- if (sa->sa_family == AF_INET) {
- return &(((struct sockaddr_in*)sa)->sin_addr);
- }
- return &(((struct sockaddr_in6*)sa)->sin6_addr);
- }
- int main(int argc, char *argv[])
- {
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
- fprintf(stderr, "WSAStartup failed.\n");
- exit(1);
- }
- struct sockaddr_storage their_addr;
- socklen_t addr_size;
- struct addrinfo hints, *res;
- int sockfd, new_fd, len, numbytes;
- len = 100;
- char s[100];
- char buf[100];
- memset(&hints, 0, sizeof hints);
- hints.sin_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_PASSIVE; // fill in my IP for me
- getaddrinfo(NULL, "3490", &hints, &res);
- cout << "Getaddrinfo error" << endl;
- sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if(sockfd == -1)
- {
- cout << "Socket error" << endl;
- return 0;
- }
- if(bind(sockfd, res->ai_addr, res->ai_addrlen) == -1)
- {
- cout << "Bind error" << endl;
- return 0;
- }
- listen(sockfd, BACKLOG);
- addr_size = sizeof their_addr;
- new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
- while(1)
- {
- /*perror("Connection status");
- int numbytes = recv(new_fd, buf, len, 0);
- perror("Message status");
- buf[numbytes] = '\0';
- printf("Client said: %s\n",buf);
- string what = buf;
- int msglen = strlen(msg);
- int bytes_sent = send(new_fd, msg, msglen, 0);
- cout << "bytes sent back to client: " << bytes_sent << endl;*/
- closesocket(new_fd);
- }
- closesocket(sockfd);
- WSACleanup();
- return 0;
- }
Add Comment
Please, Sign In to add comment