| | |
Establishing A connection between Three programs.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Solved Threads: 0
Hello Everyone,
I'm trying to exchange messages between three different programs, I was able to establish a connection between the Server and Client A programs, but when I added code to include client B program I got an infinite loop. I've enclosed segments of my code. I would really appreciate any help I can get.
Thanks in advance.
Server(Passive connection):
=====================
WSADATA WsaDat;
SOCKET Socket;
SOCKET TempSock = SOCKET_ERROR;
int portnum = 50;
char String[50];
int retval = SOCKET_ERROR;
SOCKADDR_IN SockAddr;
HOSTENT *hp;
char ServerName[255];
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
cout <<"\n WSA Initialization Failed";
cout.flush();
}
// Clear the Address
memset(&SockAddr, 0, sizeof(SOCKADDR_IN));
// Get Local Computer Name
gethostname(ServerName, sizeof(ServerName));
// Get Local Computer Address Information
hp = gethostbyname(ServerName);
if ( hp == NULL)
{
cout <<"\n Attempt to retreive computer infromation failed";
cout.flush();
return INVALID_SOCKET;
}
// This is the host address
SockAddr.sin_family = hp->h_addrtype;
// This is the port number
SockAddr.sin_port = htons(portnum);
// Create the Socket
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
cout <<"\n Socket Creation Failed";
cout.flush();
return INVALID_SOCKET;
}
// Binding the Address
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout <<"\n Attempt to bind failed";
cout.flush();
return INVALID_SOCKET;
}
// Listen to Incoming Requests and Return a Valid Socket
listen(Socket, 40);
// Accepting Incoming Requests
while (TempSock == SOCKET_ERROR)
{
TempSock =accept(Socket, NULL, NULL);
}
Socket = TempSock;
// Message exchange with Client A comes here
closesocket(TempSock);
closesocket(Socket);
==========================================================
Client A (Active connection):
======================
// ------- Initializing WSA --------
InitializingWSA(WsaDat);
// ------- Store Information About the Server -----
hostEntry = gethostbyname("station-1");
if(!hostEntry)
{
cout <<"\nError getting the host name";
cout.flush();
WSACleanup();
}
// ------- Create the Socket --------
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
cout <<"\n Socket Creation Failed";
cout.flush();
}
// ------- Binding the Socket --------
// I want an internet type connection (TCP/IP)
ServerInfo.sin_family = AF_INET;
ServerInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
// I want to use port 50
ServerInfo.sin_port = htons(50);
// Connect to the server
if (connect(Socket, (SOCKADDR *)(&ServerInfo), sizeof(ServerInfo)) != 0)
{
cout <<"\n Failed to Establish Connection with the Server";
cout.flush();
}
// Message exchange with the SERVER takes place here
// Close the Connection on the Client side
closesocket(Socket);
// Attempt to connect to client B
// ------- Initializing WSA ------
InitializingWSA(WsaDat);
// ------- Store Information About the Server -----
hostEntry2 = gethostbyname("station-1");
if(!hostEntry2)
{
cout <<"\nError getting the host name";
cout.flush();
WSACleanup();
}
// ------- Create the Socket --------
Socket2 = socket(AF_INET, SOCK_STREAM, 0);
if (Socket2 == INVALID_SOCKET)
{
cout <<"\n Socket 2 Creation Failed";
cout.flush();
}
// ------- Binding the Socket --------
// I want an internet type connection (TCP/IP)
ClientBInfo.sin_family = AF_INET;
ClientBInfo.sin_addr = *((LPIN_ADDR)*hostEntry2->h_addr_list);
// I want to use port 60
ClientBInfo.sin_port = htons(60);
if (connect(Socket, (SOCKADDR *)(&ClientBInfo), sizeof(ClientBInfo)) != 0)
{
cout <<"\n Failed to Establish Connection with the Client B";
cout.flush();
}
retval= SOCKET_ERROR;
char String22[50] = "Hello";
cout <<"\n String is:"<< String22 << endl;
cout.flush();
while (retval == SOCKET_ERROR)
{
retval = send(Socket2, String22, strlen(String22) +1,0);
if ( retval == 0)
{
cout <<"\n Connection closed at other end\n";
cout.flush();
break;
}
}
closesocket(Socket2);
==========================================================
Client B(Passive Connection):
======================
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
cout <<"\n WSA Initialization Failed";
cout.flush();
}
// Clear the Address
memset(&SockAddr, 0, sizeof(SOCKADDR_IN));
// Get Local Computer Name
gethostname(ClientBname, sizeof(ClientBname));
// Get Local Computer Address Information
hp = gethostbyname(ClientBname);
if ( hp == NULL)
{
cout <<"\n Attempt to retreive computer infromation failed";
cout.flush();
return INVALID_SOCKET;
}
// This is the host address
SockAddr.sin_family = hp->h_addrtype;
// This is the port number
SockAddr.sin_port = htons(60);
// Create the Socket
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket== INVALID_SOCKET)
{
cout <<"\n Socket Creation Failed";
cout.flush();
return INVALID_SOCKET;
}
// Binding the Address
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout <<"\n Attempt to bind failed";
cout.flush();
return INVALID_SOCKET;
}
// Listen to Incoming Requests and Return a Valid Socket
listen(Socket 40);
// Accepting Incoming Requests
while (TempSock == SOCKET_ERROR)
{
TempSock =accept(Socket, NULL, NULL);
}
Socket = TempSock;
retval= SOCKET_ERROR;
while (retval == SOCKET_ERROR)
{
retval = recv(Socket, String, 50,0);
if (retval == 0)
{
cout <<"\n Connection closed at other end";
cout.flush();
break;
}
}
cout << "\n Contents of the message:" << String << endl;
cout.flush();
// Close the Connection on the Server side
closesocket(TempSock);
closesocket(Socket);
I'm trying to exchange messages between three different programs, I was able to establish a connection between the Server and Client A programs, but when I added code to include client B program I got an infinite loop. I've enclosed segments of my code. I would really appreciate any help I can get.
Thanks in advance.
Server(Passive connection):
=====================
WSADATA WsaDat;
SOCKET Socket;
SOCKET TempSock = SOCKET_ERROR;
int portnum = 50;
char String[50];
int retval = SOCKET_ERROR;
SOCKADDR_IN SockAddr;
HOSTENT *hp;
char ServerName[255];
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
cout <<"\n WSA Initialization Failed";
cout.flush();
}
// Clear the Address
memset(&SockAddr, 0, sizeof(SOCKADDR_IN));
// Get Local Computer Name
gethostname(ServerName, sizeof(ServerName));
// Get Local Computer Address Information
hp = gethostbyname(ServerName);
if ( hp == NULL)
{
cout <<"\n Attempt to retreive computer infromation failed";
cout.flush();
return INVALID_SOCKET;
}
// This is the host address
SockAddr.sin_family = hp->h_addrtype;
// This is the port number
SockAddr.sin_port = htons(portnum);
// Create the Socket
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
cout <<"\n Socket Creation Failed";
cout.flush();
return INVALID_SOCKET;
}
// Binding the Address
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout <<"\n Attempt to bind failed";
cout.flush();
return INVALID_SOCKET;
}
// Listen to Incoming Requests and Return a Valid Socket
listen(Socket, 40);
// Accepting Incoming Requests
while (TempSock == SOCKET_ERROR)
{
TempSock =accept(Socket, NULL, NULL);
}
Socket = TempSock;
// Message exchange with Client A comes here
closesocket(TempSock);
closesocket(Socket);
==========================================================
Client A (Active connection):
======================
// ------- Initializing WSA --------
InitializingWSA(WsaDat);
// ------- Store Information About the Server -----
hostEntry = gethostbyname("station-1");
if(!hostEntry)
{
cout <<"\nError getting the host name";
cout.flush();
WSACleanup();
}
// ------- Create the Socket --------
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
cout <<"\n Socket Creation Failed";
cout.flush();
}
// ------- Binding the Socket --------
// I want an internet type connection (TCP/IP)
ServerInfo.sin_family = AF_INET;
ServerInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
// I want to use port 50
ServerInfo.sin_port = htons(50);
// Connect to the server
if (connect(Socket, (SOCKADDR *)(&ServerInfo), sizeof(ServerInfo)) != 0)
{
cout <<"\n Failed to Establish Connection with the Server";
cout.flush();
}
// Message exchange with the SERVER takes place here
// Close the Connection on the Client side
closesocket(Socket);
// Attempt to connect to client B
// ------- Initializing WSA ------
InitializingWSA(WsaDat);
// ------- Store Information About the Server -----
hostEntry2 = gethostbyname("station-1");
if(!hostEntry2)
{
cout <<"\nError getting the host name";
cout.flush();
WSACleanup();
}
// ------- Create the Socket --------
Socket2 = socket(AF_INET, SOCK_STREAM, 0);
if (Socket2 == INVALID_SOCKET)
{
cout <<"\n Socket 2 Creation Failed";
cout.flush();
}
// ------- Binding the Socket --------
// I want an internet type connection (TCP/IP)
ClientBInfo.sin_family = AF_INET;
ClientBInfo.sin_addr = *((LPIN_ADDR)*hostEntry2->h_addr_list);
// I want to use port 60
ClientBInfo.sin_port = htons(60);
if (connect(Socket, (SOCKADDR *)(&ClientBInfo), sizeof(ClientBInfo)) != 0)
{
cout <<"\n Failed to Establish Connection with the Client B";
cout.flush();
}
retval= SOCKET_ERROR;
char String22[50] = "Hello";
cout <<"\n String is:"<< String22 << endl;
cout.flush();
while (retval == SOCKET_ERROR)
{
retval = send(Socket2, String22, strlen(String22) +1,0);
if ( retval == 0)
{
cout <<"\n Connection closed at other end\n";
cout.flush();
break;
}
}
closesocket(Socket2);
==========================================================
Client B(Passive Connection):
======================
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
cout <<"\n WSA Initialization Failed";
cout.flush();
}
// Clear the Address
memset(&SockAddr, 0, sizeof(SOCKADDR_IN));
// Get Local Computer Name
gethostname(ClientBname, sizeof(ClientBname));
// Get Local Computer Address Information
hp = gethostbyname(ClientBname);
if ( hp == NULL)
{
cout <<"\n Attempt to retreive computer infromation failed";
cout.flush();
return INVALID_SOCKET;
}
// This is the host address
SockAddr.sin_family = hp->h_addrtype;
// This is the port number
SockAddr.sin_port = htons(60);
// Create the Socket
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket== INVALID_SOCKET)
{
cout <<"\n Socket Creation Failed";
cout.flush();
return INVALID_SOCKET;
}
// Binding the Address
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
cout <<"\n Attempt to bind failed";
cout.flush();
return INVALID_SOCKET;
}
// Listen to Incoming Requests and Return a Valid Socket
listen(Socket 40);
// Accepting Incoming Requests
while (TempSock == SOCKET_ERROR)
{
TempSock =accept(Socket, NULL, NULL);
}
Socket = TempSock;
retval= SOCKET_ERROR;
while (retval == SOCKET_ERROR)
{
retval = recv(Socket, String, 50,0);
if (retval == 0)
{
cout <<"\n Connection closed at other end";
cout.flush();
break;
}
}
cout << "\n Contents of the message:" << String << endl;
cout.flush();
// Close the Connection on the Server side
closesocket(TempSock);
closesocket(Socket);
![]() |
Similar Threads
- Create Database Connection in the C#.net (C#)
- BSc Computer Science final year project (Computer Science)
- VB.Net and sql server connection (VB.NET)
- Connection Problems: Cause unknown (Networking Hardware Configuration)
- external database connection (MySQL)
- SQL Server Connection problem (ASP.NET)
- establishing connection with more than one database (VB.NET)
- direct remote sql connection (Visual Basic 4 / 5 / 6)
- Connection not recognized (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: reading input ... quick C++ question ...
- Next Thread: how to do something special with this question
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





