| | |
please help, winsock2 10061 error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
hello friends,
I have two program: receive.cpp and send.cpp
They re very simple, when compiling there is no error in both.
I only wanna see them to talk to each other but when run these progs,
both of them gives 10061 error. Below the codes, firstly receive.cpp:
and the sender.cpp
where I do wrong?
I have two program: receive.cpp and send.cpp
They re very simple, when compiling there is no error in both.
I only wanna see them to talk to each other but when run these progs,
both of them gives 10061 error. Below the codes, firstly receive.cpp:
C++ Syntax (Toggle Plain Text)
//******************* SERVER PROG **************** #include <winsock2.h> #include <stdio.h> #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "27015" int __cdecl main() { WSADATA wsaData; int iResult; SOCKET ConnectSocket; struct sockaddr_in clientService; char *sendbuf = "bu bir testtir."; char recvbuf[DEFAULT_BUFLEN]; int recvbuflen = DEFAULT_BUFLEN; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) { printf("WSAStartup failed: %d\n", iResult); return 1; } ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError() ); WSACleanup(); return 1; } clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" ); clientService.sin_port = htons( 27015 ); iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ); if ( iResult == SOCKET_ERROR) { closesocket (ConnectSocket); printf("Unable to connect to server: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(ConnectSocket); //WSACleanup(); return 1; } printf("Bytes Sent: %ld\n", iResult); iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ) printf("Bytes received: %d\n", iResult); else if ( iResult == 0 ) printf("Connection closed\n"); else printf("recv failed: %d\n", WSAGetLastError()); } while( iResult > 0 ); closesocket(ConnectSocket); WSACleanup(); return 0; }
and the sender.cpp
C++ Syntax (Toggle Plain Text)
//*******************************CLIENT********************************* #include <winsock2.h> #include <stdio.h> #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT 27015 int main() { int iResult; WSADATA wsaData; SOCKET ConnectSocket; struct sockaddr_in clientService; int recvbuflen = DEFAULT_BUFLEN; char *sendbuf = "Client: sending data test"; char recvbuf[DEFAULT_BUFLEN] = ""; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) { printf("WSAStartup failed with error: %d\n", iResult); return 1; } ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { printf("socket failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" ); clientService.sin_port = htons( DEFAULT_PORT ); iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ); if (iResult == SOCKET_ERROR) { printf( "connect failed with error: %d\n", WSAGetLastError() ); closesocket(ConnectSocket); WSACleanup(); return 1; } iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { printf("send() failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } printf("Bytes Sent: %d\n", iResult); iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ) printf("Bytes received: %d\n", iResult); else if ( iResult == 0 ) printf("Connection closed\n"); else printf("recv failed with error: %d\n", WSAGetLastError()); } while( iResult > 0 ); closesocket(ConnectSocket); WSACleanup(); return 0; }
where I do wrong?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- winsock problem (C++)
- Can't connect to MySQL server on 'localhost' (10061) (PHP)
- Server/Client timeout error (C)
- WinSock problems. (C++)
- DNS ERROR>>>CANNOT CONNECT TO SECURE SITES (Viruses, Spyware and other Nasties)
- Help with this? (Windows NT / 2000 / XP)
- Winsock2.h and Platform SDK (C)
- Error 317, Stealth.Hjack Virus - Tried your tips but... (Viruses, Spyware and other Nasties)
- Outlook express 6 - Can't get messages from POP3 servers (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Concatenate strings/chars in a loop.
- Next Thread: ofstream corrupting file when program terminated
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






