| | |
please help, winsock2 10061 error
![]() |
•
•
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 binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






