# Socket programming



## Ria_M (Jul 17, 2007)

Hi guys
Im trying to write a server socket program. I created the socket and it binds fine. I cant get any connection accepted. Im listening on port 5545. And im supposed to get some data on the same port. Unless im listening on the wrong port or something. I just specify the port number as htons(5545). Am i missing some conversion or something?please help!


----------



## MattBro (Nov 11, 2006)

Hi Ria,

Show me your code and I'll tell you what's up


----------



## Ria_M (Jul 17, 2007)

#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>


using namespace std;

int main()//(char **argv)

{

WORD wVersionRequested;
WSADATA wsaData;
int wsaerr; //error code//

// Using MAKEWORD macro, Winsock version request 2.2

wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);

if (wsaerr != 0)
{
/* Tell the user that we could not find a usable WinSock DLL.*/

printf("The Winsock dll not found!\n");
return 0;
}
else
{
printf("The Winsock dll found!\n");
printf("The status: %s.\n", wsaData.szSystemStatus);
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we requested */

if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 )

{
/* Tell the user that we could not find a usable WinSock DLL.*/

printf("The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
WSACleanup();
return 0; 
}
else
{
printf("The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
printf("The highest version this dll can support: %u.%u\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
} 

//Create A Socket Object- m_socket//

SOCKET m_socket;

// Internet Address Family,Streaming Sockets,TCP/IP Protocol//
m_socket=socket(AF_INET,SOCK_STREAM,6);

//Error-Checking-if socket valid//

if (m_socket==INVALID_SOCKET)
{
printf("Error at socket(): %ld\n",WSAGetLastError());
WSACleanup();
return 0;
}
else
{
printf("Socket () is OK! \n");
}


//binding///


//creating object service and setting its properties//

sockaddr_in service;

service.sin_family = AF_INET; //internet address family//
service.sin_addr.s_addr=inet_addr("192.168.100.1"); //assumes ip address of host connected to//
service.sin_port = htons(5545);


if (bind(m_socket,(SOCKADDR*)&service,sizeof(service)) == SOCKET_ERROR)
{
printf("bind() failed %ld.\n",WSAGetLastError());
closesocket(m_socket);
}
else
{
printf("bind() is OK!\n");
}

//listening//

if (listen(m_socket,5) == SOCKET_ERROR)
printf("listen(): Error Listening on Socket() %ld.\n",WSAGetLastError());
else
printf("listen() is OK, Waiting for Connection\n");

//accepting connection//

//create temp socket for accepting connections//

SOCKET tempsocket;


printf("Waiting for Client to connect...\n");

//Loop to check for requests-if found request-handled by accept function//

//while (1)
//{ 
tempsocket= accept(m_socket,(struct sockaddr*)0,(int*)0);//stuck here

if (tempsocket == INVALID_SOCKET)
{
printf("socket error %ld\n",WSAGetLastError());
closesocket(m_socket);
WSACleanup();
}
else
{ 


//else accept connection - control transferred from temp to m socket//

printf("Client Connected\n"); 

}

m_socket=tempsocket;

// break; // stop checking for connections//
//}

int bytesRecv; 
char recvbuf[2048];

do
{
if ((bytesRecv=recv(m_socket,recvbuf,strlen(recvbuf),0)) < 0)
printf("Error Reading Stream\n");
else
{
if (bytesRecv!=0)
{
printf("Received %ld.\n",recvbuf);
}
}
} 
while (bytesRecv >0);

closesocket(m_socket);
system("pause");
return 0;
}


----------



## MattBro (Nov 11, 2006)

Alright, that looks fine. Now show me your client-side code.


----------



## Ria_M (Jul 17, 2007)

Hi

I don't know if this makes sense but on the project that I'm working on we were only required to write a server socket. I'm working on an image sampling engine, where we were given a equipment that gets images from a camera and sends them out at a specific port. So, to view these images on the pc, I had to write a server socket since the pc was the server.

Thanks 

Ria


----------



## MattBro (Nov 11, 2006)

Are you sure they were refering to network ports? A camera usually would interface over USB.


----------



## Ria_M (Jul 17, 2007)

Hi

Yeah, the camera's connected to a box that obtains the images. I need to read the images stored in this box.It contains a linux kernel and frame grabber.

Thanks


----------

