viggu
June 25th, 2007, 12:26 AM
hello all
i am a beginner to socket programming. i tried the following code. it compiles , but my client wont connect to server at all... i do not know what the problem is...
--------------------- Client ---------------------------------------------
#include "stdafx.h"
#include <winsock2.h>
#include <conio.h>
#include <iostream>
#include <stdio.h>
#pragma comment(lib,"Ws2_32.lib")
#define WSVERS MAKEWORD(2,0)
#define qlen 10
int main(int argc, char* argv[])
{
char temp[512];
const char* servername="127.0.0.1";
const char* transport = "tcp";
SOCKET client,newsock;
WSADATA wsaData;
sockaddr_in local;
struct protoent *ppe;
int wsaret=WSAStartup(WSVERS, &wsaData);
if(wsaret!=0)
{
return 0;
}
local.sin_family=AF_INET;
local.sin_addr.s_addr=inet_addr(servername);;
local.sin_port=htons(u_short(220447));
ppe = getprotobyname(transport);
client=socket(AF_INET,SOCK_STREAM,ppe->p_proto);
printf("Socket created\n");
if(client==INVALID_SOCKET)
{
return 0;
}
int fromlen=sizeof(struct sockaddr_in);
if(newsock = connect(client, (struct sockaddr *)&local, sizeof(local))== SOCKET_ERROR){
return 0;
}
int cc = recv(newsock, temp, 511, 0);
while((cc != SOCKET_ERROR) && cc>0)
{
recv(newsock, temp, 511, 0);
(void)closesocket(newsock);
}
closesocket(client);
WSACleanup();
return 0;
}
--------------------------------------------------------------------------------------
------------------- server program --------------------------------------
// project-sercl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <conio.h>
#include <iostream>
#pragma comment(lib,"Ws2_32.lib")
#define WSVERS MAKEWORD(2,0)
#define qlen 10
int main(int argc, char* argv[])
{
SOCKET server,client;
WSADATA wsaData;
sockaddr_in local,from;
int wsaret=WSAStartup(WSVERS, &wsaData);
if(wsaret!=0)
{
return 0;
}
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons((u_short)220447);
server=socket(AF_INET,SOCK_STREAM,0);
printf("Socket created\n");
if(server==INVALID_SOCKET)
{
return 0;
}
printf("binding Done\n");
if(bind(server,(struct sockaddr *)&local,sizeof(local))!=0)
{
return 0;
}
printf("Listen to socket\n");
if(listen(server,qlen)!=0)
{
return 0;
}
int fromlen=sizeof(struct sockaddr_in);
while(true)
{
char temp[512];
client=accept(server,(struct sockaddr*)&from,&fromlen);
memset(temp, 0, sizeof(temp));
printf("Enter some stuff here\n");
gets_s(temp, 511);
send(client,temp, 511,0);
(void)closesocket(client);
}
closesocket(server);
WSACleanup();
return 0;
}
-------------------------------------------------------------------------------------
All i am trying to do is connect to server and exchange a messge .. this is just to start with my project in socket programming ... i am unable to find what the problem is ... when i compile and run the client.. it goes off ... i am not able to see the command prompt....
what can be the problem ? i am sitting with this program for so many hours and finding no way out of this...
any help is greatly appreciated and would help me get going with my project.
regards
vignesh
Error Message:
-------------------------------------------
'client.exe': Loaded 'C:\Documents and Settings\vsathiam\My Documents\Visual Studio 2005\Projects\client\debug\client.exe', Symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\mswsock.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\hnetcfg.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\csauser.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\AMInit.dll', Binary was not built with debug information.
The thread 'Win32 Thread' (0x164c) has exited with code 0 (0x0).
'client.exe': Loaded 'C:\WINDOWS\system32\wshtcpip.dll', No symbols loaded.
The program '[4208] client.exe: Native' has exited with code 0 (0x0).
----------------------------------------------------------------------------------------
i am a beginner to socket programming. i tried the following code. it compiles , but my client wont connect to server at all... i do not know what the problem is...
--------------------- Client ---------------------------------------------
#include "stdafx.h"
#include <winsock2.h>
#include <conio.h>
#include <iostream>
#include <stdio.h>
#pragma comment(lib,"Ws2_32.lib")
#define WSVERS MAKEWORD(2,0)
#define qlen 10
int main(int argc, char* argv[])
{
char temp[512];
const char* servername="127.0.0.1";
const char* transport = "tcp";
SOCKET client,newsock;
WSADATA wsaData;
sockaddr_in local;
struct protoent *ppe;
int wsaret=WSAStartup(WSVERS, &wsaData);
if(wsaret!=0)
{
return 0;
}
local.sin_family=AF_INET;
local.sin_addr.s_addr=inet_addr(servername);;
local.sin_port=htons(u_short(220447));
ppe = getprotobyname(transport);
client=socket(AF_INET,SOCK_STREAM,ppe->p_proto);
printf("Socket created\n");
if(client==INVALID_SOCKET)
{
return 0;
}
int fromlen=sizeof(struct sockaddr_in);
if(newsock = connect(client, (struct sockaddr *)&local, sizeof(local))== SOCKET_ERROR){
return 0;
}
int cc = recv(newsock, temp, 511, 0);
while((cc != SOCKET_ERROR) && cc>0)
{
recv(newsock, temp, 511, 0);
(void)closesocket(newsock);
}
closesocket(client);
WSACleanup();
return 0;
}
--------------------------------------------------------------------------------------
------------------- server program --------------------------------------
// project-sercl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <conio.h>
#include <iostream>
#pragma comment(lib,"Ws2_32.lib")
#define WSVERS MAKEWORD(2,0)
#define qlen 10
int main(int argc, char* argv[])
{
SOCKET server,client;
WSADATA wsaData;
sockaddr_in local,from;
int wsaret=WSAStartup(WSVERS, &wsaData);
if(wsaret!=0)
{
return 0;
}
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons((u_short)220447);
server=socket(AF_INET,SOCK_STREAM,0);
printf("Socket created\n");
if(server==INVALID_SOCKET)
{
return 0;
}
printf("binding Done\n");
if(bind(server,(struct sockaddr *)&local,sizeof(local))!=0)
{
return 0;
}
printf("Listen to socket\n");
if(listen(server,qlen)!=0)
{
return 0;
}
int fromlen=sizeof(struct sockaddr_in);
while(true)
{
char temp[512];
client=accept(server,(struct sockaddr*)&from,&fromlen);
memset(temp, 0, sizeof(temp));
printf("Enter some stuff here\n");
gets_s(temp, 511);
send(client,temp, 511,0);
(void)closesocket(client);
}
closesocket(server);
WSACleanup();
return 0;
}
-------------------------------------------------------------------------------------
All i am trying to do is connect to server and exchange a messge .. this is just to start with my project in socket programming ... i am unable to find what the problem is ... when i compile and run the client.. it goes off ... i am not able to see the command prompt....
what can be the problem ? i am sitting with this program for so many hours and finding no way out of this...
any help is greatly appreciated and would help me get going with my project.
regards
vignesh
Error Message:
-------------------------------------------
'client.exe': Loaded 'C:\Documents and Settings\vsathiam\My Documents\Visual Studio 2005\Projects\client\debug\client.exe', Symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\mswsock.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\hnetcfg.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\csauser.dll', No symbols loaded.
'client.exe': Loaded 'C:\WINDOWS\system32\AMInit.dll', Binary was not built with debug information.
The thread 'Win32 Thread' (0x164c) has exited with code 0 (0x0).
'client.exe': Loaded 'C:\WINDOWS\system32\wshtcpip.dll', No symbols loaded.
The program '[4208] client.exe: Native' has exited with code 0 (0x0).
----------------------------------------------------------------------------------------