please help, winsock2 10061 error
Please support our C++ advertiser: Programming Forums
![]() |
•
•
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:
//******************* 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
//*******************************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?
![]() |
Similar Threads
Other Threads in the C++ Forum
- 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 / 2003)
- 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: help! how to implement a copy constructor in c++ for ADT queues??
- Next Thread: how do I have txt file be read by the compiler?
•
•
•
•
Views: 2135 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode