Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int initSockets() {
- WSADATA wsaData;
- struct addrinfo *result = NULL, *ptr = NULL, hints;
- SOCKET ClientSocket;
- SOCKET ListenSocket;
- int iResult;
- char recvbuf[100];
- int fromlen = 50;
- unsigned short port;
- int recvbuflen = 50;
- struct sockaddr_in local, from;
- int socket_type = SOCK_STREAM;
- // Initialize Winsock
- printf("in initsockets\n\n");
- iResult = WSAStartup(0x202, &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed: %d\n", iResult);
- return -1;
- }
- printf("2\n");
- ZeroMemory(&hints, sizeof (hints));
- local.sin_family = AF_INET;
- port = (unsigned short)DEFAULT_PORT;
- local.sin_addr.s_addr = INADDR_ANY;
- local.sin_port = (unsigned short)DEFAULT_PORT;
- // Resolve the local address and port to be used by the server
- printf("test");
- ListenSocket = INVALID_SOCKET;
- ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
- if (ListenSocket == INVALID_SOCKET) {
- printf("Error at socket(): %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- iResult = bind( ListenSocket, (struct sockaddr*)&local, sizeof(local));
- if (iResult == SOCKET_ERROR) {
- printf("bind failed with error: %d\n", WSAGetLastError());
- freeaddrinfo(result);
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- printf("here\n");
- freeaddrinfo(result);
- if ( listen( ListenSocket, 10 ) == SOCKET_ERROR ) {
- printf( "Listen failed with error: %ld\n", WSAGetLastError() );
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- ClientSocket = INVALID_SOCKET;
- while(1) {
- // Accept a client socket
- printf("s\n");
- ClientSocket = accept(ListenSocket, (struct sockaddr*)&from, &fromlen);
- printf("f");
- if (ClientSocket == INVALID_SOCKET) {
- printf("accept failed: %d\n", WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0) {
- printf("Bytes received: %d\n", iResult);
- // Echo the buffer back to the sender
- printf("HAAAAH\n\n");
- } else if (iResult == 0)
- printf("Connection closing...\n");
- else {
- printf("recv failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- iResult = shutdown(ClientSocket, SD_SEND);
- printf("\ntests2\n");
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }
- printf("down here");
- }
- printf("111111\n");
- }
- // cleanup
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement