Ok, I've recently started experimenting with winsock in C++. I had intended to write a server-type program in C++, and then use MSVB 6.0 for the client side. I can set up a socket, open it, and accept connection from a C++ server to a C++ client. However, If I write the server in C++ and try to conenct using a client program I wrote in VB onto the same port, the connection is not found. My understanding is that winsock allowed 2 computers to communicate regardless of the underlying language. Here I will post my code in both languages. I understand that this is not a VB board, was just hoping someone might be able to shed some light onto the situation. BTW the code is not all my own, so I make no claims to ownership of it.
C++ Server:
//Assumes:you MUST add the "mpr.lib wsock32.lib "
#include <iostream.h>
#include <winsock.h>
#include <string.h>
// Prototypes
/////////////////////
// Functions
/////////////////////
// mian Function
int main()
{
while(true)
{
WSADATA WsaDat; //Declares what is necessary to use winsock
int ip1(127), ip2(0), ip3(0), ip4(1); //Holders for IP address, 127.0.0.1 used for simplicity
if (WSAStartup(MAKEWORD(1,1), &WsaDat) != 0) //Initiallizes the WSA
{
cout << "WSA initialization failed.";
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
return 0;
}
SOCKET mysocket; //Our Handy Dandy Socket
mysocket = socket(AF_INET, SOCK_STREAM, 0); //Sets the properties of our socket
if (mysocket == INVALID_SOCKET) //Checks to see if it created correctly
{
cout << "Socket creation failed!";
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
return 0;
}
SOCKADDR_IN SockAddr; //this keeps track of the socket's address info
SockAddr.sin_port = 10119;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = ip1;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = ip2;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = ip3;
SockAddr.sin_addr.S_un.S_un_b.s_b4 = ip4;
char Servername[20], CCommand[20]; // self-explanatory
strcpy(Servername,"Dynastic1");
// bind to IP address. . .
if (bind(mysocket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout << "Attempt to bind failed" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
return 0;
}
cout << "Waiting for client. . ." << endl;
//go into "Listen" mode. . .
listen(mysocket, 1);
//when request received to connect, accept it
SOCKET TempSock = SOCKET_ERROR;
while (TempSock == SOCKET_ERROR)
{
TempSock = accept(mysocket, NULL, NULL);
}
mysocket = TempSock;
//send your screen name to the client
cout << "Client Found. Sending data" << endl;
send(mysocket, Servername, sizeof(Servername), 0);
bool acomplish;
acomplish = false;
while(acomplish == false)
{
int WaitforCommand = SOCKET_ERROR;
while (WaitforCommand == SOCKET_ERROR)
{
WaitforCommand = recv(mysocket, CCommand, 20, 0);
if ((WaitforCommand == 0)||(WaitforCommand == WSAECONNRESET))
{
cout << "Connection closed at the other end while receiving screen name! RATS!" << endl;
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
return 0;
}
}
//////////////////////////////////////////////////
///And here is where all hte coding takes place, for debug only///
//////////////////////////////////////////////////
if ((strcmp(CCommand, "bob")) ==0)
{
cout << "They sent bob" << endl << endl;
acomplish = true;
}
else
cout << "Didnt send bob, so thats it" << endl;
acomplish = true;
}
closesocket(mysocket);
}
// end prog
cout << "Closing. . .";
Sleep(MAKEWORD(10,30));
return 0;
}
************************************************
VB Client, in the formload with only a single component, a winsock activeX with name sckClient:
Private Sub Form_Load()
sckClient.RemoteHost = "127.0.0.1"
sckClient.RemotePort = 10119
sckClient.Connect
End Sub