Hello!
I am trying to establish a connection between two programs via sockets on the same computer.
I have two files (and two Visual C++ 6.0 running). The connection is established, bytes are sent, but they are not received.
Help me please. What should I change!
First file:
Code:
#include <stdio.h>
#include "winsock2.h"
SOCKET ListenSocket;
SOCKADDR_IN service;
SOCKET AcceptSocket;
SOCKET ConnectSocket;
SOCKADDR_IN clientService;
//----------------------
// Declare and initialize variables.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";
void Accept() {
//----------------------
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
//----------------------
// Create a SOCKET for listening for
// incoming connection requests.
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port for the socket that is being bound.
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.18.2.15");
service.sin_port = htons(27015);
if (bind( ListenSocket,
(SOCKADDR*) &service,
sizeof(service)) == SOCKET_ERROR) {
printf("bind() failed.\n");
closesocket(ListenSocket);
return;
}
//----------------------
// Listen for incoming connection requests.
// on the created socket
if (listen( ListenSocket, 1 ) == SOCKET_ERROR)
printf("Error listening on socket.\n");
//----------------------
// Create a SOCKET for accepting incoming requests.
printf("Waiting for client to connect...\n");
//----------------------
// Accept the connection.
while(1) {
AcceptSocket = SOCKET_ERROR;
while( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( ListenSocket, NULL, NULL );
}
printf("Client connected.\n");
ListenSocket = AcceptSocket;
break;
}
WSACleanup();
return;
}
void SendReceive() {
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( ConnectSocket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv == -1)
{
printf( "Bytes Recv: %ld\n", bytesRecv );
printf("Error at socket(): %ld\n", WSAGetLastError());
}
}
WSACleanup();
return;
}
void main ()
{
Accept();
SendReceive();
}
Second file:
Code:
#include <stdio.h>
#include "winsock2.h"
SOCKET ListenSocket;
SOCKADDR_IN service;
SOCKET AcceptSocket;
SOCKET ConnectSocket;
SOCKADDR_IN clientService;
//----------------------
// Declare and initialize variables.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";
void SendReceive() {
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.18.2.15" );
clientService.sin_port = htons( 27015 );
//----------------------
// Connect to server.
if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
//----------------------
// Send and receive data.
bytesSent = send( ConnectSocket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
WSACleanup();
return;
}
void main ()
{
SendReceive();
}
Thanx a lot!