Hi folks,
I am having some trouble with some extremely basic TCP socket programming.
My mock TCP server runs on ps3(YDL) and my client runs on WinXP.
I have statically configured ip addresses on both the machines. I am able to ping from one machine to the other.
However, in my trivial TCP socket programs, the client times out when it tries to connect to the server. I am unable to figure out why. I have even brought down the firewall on my windows machine. Is there any configuration I am missing? It works fine if both the client and server are on the ps3.
Could some one please take a quick look at my stripped down version of code(below) and give me a few pointers as to what I'm doing wrong?
Its a little embarassing , but I am stumped.
Best,
Ashok
Server code:
----------------
#include<sys/socket.h>
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include <netdb.h>
#include<stdlib.h>
#include<unistd.h>
#include <string.h>
#define SERVER_TCP_PORT 7777
int SocketId;
int TempSockId;
int ClientSize;
struct sockaddr_in Server;
struct sockaddr_in Client;
int main()
{
//create server socket
if((SocketId=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
fprintf(stderr,"cant create a socket");
exit(1);
}
memset((char *)&Server,0, sizeof(struct sockaddr_in));
//initialize the server sockaddr_in structure members
Server.sin_family = AF_INET;
Server.sin_port = htons(SERVER_TCP_PORT);
Server.sin_addr.s_addr = inet_addr("11.11.11.2");
//bind the socket to the well known tcp port for the server
if(bind(SocketId,(struct sockaddr *)&Server,sizeof(Server))<0)
{
fprintf(stderr,"cant bind socket ");
exit(1);
}
printf("Server socket created and bound\n");
listen(SocketId,3); //the server buffers the incomming connection requests
ClientSize = sizeof(Client);
printf("Waiting for client\n");
//accept the connection and create a new temporary socket for the duratio of the cconnection
if((TempSockId = accept(SocketId,(struct sockaddr *)&Client,&ClientSize))<0)
{
fprintf(stderr,"cant accept client");
exit(1);
}
printf("Connection received\n");
close(SocketId);
close(TempSockId);
return 0;
}
Client code:
---------------
#include <assert.h>
#include <stdio.h>
#include <winsock2.h>
#include <iostream>
#define SERVER_TCP_PORT 7777
#define SOCKET_SUCCESS 0
int sd; // The socket descriptor where the server listens for new connections.
int client_len;
int new_sd; // The socket descriptor for the specific connection.;
struct sockaddr_in server; // The sockaddr_in structure for the server.
struct sockaddr_in client; // The sockaddr_in structure for the client.
int InitiateSockLibVersion(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
printf(" ERROR: The requested DLL version not supported \n");
return SOCKET_ERROR;
}
/* Confirm that the WinSock DLL supports 2.0. */
/* Note that if the DLL supports versions greater */
/* than 2.0 in addition to 2.0, it will still return */
/* 2.0 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
/* Tell the user that we could not find a usable */
printf(" ERROR: The requested DLL version not supported \n");
WSACleanup( );
return SOCKET_ERROR;
}
printf ("DEBUG:Successfully negotiated for Socket dll \n");
return SOCKET_SUCCESS;
}
int SetUpConnection()
{
int err = 0;
//create server socket
sd=socket(AF_INET ,SOCK_STREAM,IPPROTO_TCP);
if(-1 == sd)
{
err = WSAGetLastError();
fprintf(stderr,"ERROR:Cant create a socket, err number: %d\n", err);
return SOCKET_ERROR;
}
printf ("DEBUG:Socket created\n");
//initialize the server sockaddr_in structure members
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_TCP_PORT);
// initialize the server structure member sin_addr
//using the address stored in the hostent structure
server.sin_addr.s_addr = inet_addr("11.11.11.2");
/* attempt to establish connection with the server*/
if(connect(sd,(struct sockaddr *)&(server),sizeof(struct sockaddr)) != 0)
{
printf("ERROR:Connection with server failed!\n");
return SOCKET_ERROR;
}
printf("DEBUG:Client connected to URBI server\n");
return SOCKET_SUCCESS;
}
void CloseConnection()
{
closesocket(sd);
WSACleanup( );
printf("DEBUG:Socket closed.\n");
}
int main (int argc, char* argv[])
{
// Setup TCP/IP Session
if (SOCKET_SUCCESS != InitiateSockLibVersion())
{
CloseConnection();
return SOCKET_ERROR;
}
if (SOCKET_SUCCESS != SetUpConnection())
{
CloseConnection();
return SOCKET_ERROR;
}
CloseConnection();
return SOCKET_SUCCESS;
}
Trouble with simple socket programming on the ps3
-
- Posts: 2
- Joined: Thu Mar 01, 2007 9:05 pm
-
- Posts: 2
- Joined: Thu Mar 01, 2007 9:05 pm