473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Winsock programming

3
I have created a small client application with loopback address (127.0.0.1) so that same machine act as server and client. but i want to know whether we have to create seperate server application and run that seperately inorder to connect with the same machine.
Even if i run client and server application seperately client application shows the error ie., "Not able to connect program aborted".
In the server application it just run without displaying anything on the screen.
if u have any source code in C++ which is working fine u just send it to me..Im new to the socket programming So pls can anyone help me out..

Thanks,
Haroon.
Jun 11 '07 #1
3 6605
Banfa
9,065 Expert Mod 8TB
The loop back should work. I write WinSock based server applications and I test them by writing simulator programs for the remote pieces of equipment and I run them both on the same machine and the connect without problem.
Jun 11 '07 #2
Har
3
The loop back should work. I write WinSock based server applications and I test them by writing simulator programs for the remote pieces of equipment and I run them both on the same machine and the connect without problem.
Actually i have written the winsock server/client application which i will include it below.In that i have got the server waiting for too long i dont know why..and the client is not able to connect the socket..whether i have to run both the application to establish the connection throu socket?in client application im getting the error "Connect Error. Program aborted"..im using visual C++ compiler.

Client :

#include <winsock.h>
#include <iostream.h>
int gPort = 1150;
void main()
{
SOCKET lhSocket;
SOCKADDR_IN lSockAddr;
WSADATA wsaData;
int lConnect;
int lLength;
char lData[]="SendData";
if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
cout<<"Socket Initialization Error. Program aborted\n";
return;
}
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
cout<<"Invalid Socket "<<GetLastError()<<". Program Aborted\n"<<endl;
}
memset(&lSockAddr,0, sizeof(lSockAddr));
lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(gPort);
lSockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));
if(lConnect != 0)
{
cout<<"Connect Error. Program aborted\n";
return;
}
lLength = send(lhSocket,lData,strlen(lData),0);
if(lLength < strlen(lData))
{
cout<<"Send Error.\n";
}
closesocket(lhSocket);
return;
}


Server:

#include <winsock2.h>
#include <iostream.h>

#define PORT 1250
#define BUFFERSIZE 8192

typedef struct _MYSOCKET_INFORMATION {
CHAR Buffer[BUFFERSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD SendBytes;
DWORD RecvBytes;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;

BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Index);

DWORD TotalSockets = 0;
LPSOCKET_INFORMATION SocketList[FD_SETSIZE];

void main(void)
{

//some of the basic declarations required for this winsock tutorial
SOCKET ListenSocket;
SOCKET AcceptSocket;
SOCKADDR_IN InternetAddr;
WSADATA wsaData;
FD_SET Writer;
FD_SET Reader;
ULONG NonBlock;
DWORD Flags;
DWORD Total;
DWORD Ret;
DWORD i;
DWORD SendBytes;
DWORD RecvBytes;


if ((Ret = WSAStartup(MAKEWORD(2,0),&wsaData)) != 0)
{
cout<<"Start up failed";
}

// Create a socket for the winsock tutorial.

if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,
WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
cout<<"Winsock tutorial error: WSASocket() failed \n"<< WSAGetLastError();
return;
}

InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);

if (bind(ListenSocket, (SOCKADDR *) &InternetAddr, sizeof(InternetAddr))
== SOCKET_ERROR)
{
cout<<"Winsock tutorial error: Binding failed \n"<<WSAGetLastError();
return;
}

if (listen(ListenSocket, 5))
{
cout<<"Winsock tutorial error: listen failed \n"<< WSAGetLastError();
return;
}

// Change the socket mode on the listening socket from blocking to non-block

NonBlock = 1;
if (ioctlsocket(ListenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
cout<<"ioctlsocket() failed \n";
return;
}

while(TRUE)
{
// Initialize the Read and Write socket set.
FD_ZERO(&Reader);
FD_ZERO(&Writer);

// Check for connection attempts.
FD_SET(ListenSocket, &Reader);

// Set Read and Write notification for each socket based on the
// current state the buffer.

for (i = 0; i < TotalSockets; i++)
if (SocketList[i]->RecvBytes > SocketList[i]->SendBytes)
FD_SET(SocketList[i]->Socket, &Writer);
else
FD_SET(SocketList[i]->Socket, &Reader);

if ((Total = select(0, &Reader, &Writer, NULL, NULL)) == SOCKET_ERROR)
{
cout<<"Winsock tutorial error: select function returned with error \n"<< WSAGetLastError();
return;
}

// Check for arriving connections on the listening socket.
if (FD_ISSET(ListenSocket, &Reader))
{
Total--;
if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET)
{

// Set the accepted socket to non-blocking mode so the server will
// not get caught in a blocked condition on WSASends

NonBlock = 1;
if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
cout<<"Winsock tutorial error: ioctlsocket() failed with error \n", WSAGetLastError();
return;
}

if (CreateSocketInformation(AcceptSocket) == FALSE)
return;
}
else
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout<<"accept() failed with error %d\n", WSAGetLastError();
return;
}
}
}

// Check each socket for Read and Write notification for Total number of sockets

for ( i = 0; Total > 0 && i < TotalSockets; i++)
{
LPSOCKET_INFORMATION SocketInfo = SocketList[i];

// If the Reader is marked for this socket then this means data
// is available to be read on the socket.

if (FD_ISSET(SocketInfo->Socket, &Reader))
{
Total--;

SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = BUFFERSIZE;

Flags = 0;
if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
&Flags, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout<<"Winsock tutorial: Receive failed with error\n";

FreeSocketInformation(i);
}
continue;
}
else
{
SocketInfo->RecvBytes = RecvBytes;
cout<<SocketInfo->DataBuf.buf<<"\n";

// If zero bytes are received, this indicates connection is closed.
if (RecvBytes == 0)
{
FreeSocketInformation(i);
continue;
}
}
}


// If the Writer is marked on this socket then this means the internal
// data buffers are available for more data.

if (FD_ISSET(SocketInfo->Socket, &Writer))
{
Total--;

SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->SendBytes;
SocketInfo->DataBuf.len = SocketInfo->RecvBytes - SocketInfo->SendBytes;

if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout<<"Send failed with error\n";
FreeSocketInformation(i);
}

continue;
}
else
{
SocketInfo->SendBytes += SendBytes;

if (SocketInfo->SendBytes == SocketInfo->RecvBytes)
{
SocketInfo->SendBytes = 0;
SocketInfo->RecvBytes = 0;
}
}
}
}
}
}

BOOL CreateSocketInformation(SOCKET s)
{
LPSOCKET_INFORMATION SI;

cout<<"Accepted socket\n";

if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
cout<<"Winsock tutorial error: GlobalAlloc() failed\n";
return FALSE;
}

// Prepare SocketInfo structure for use.

SI->Socket = s;
SI->SendBytes = 0;
SI->RecvBytes = 0;

SocketList[TotalSockets] = SI;

TotalSockets++;

return(TRUE);
}

void FreeSocketInformation(DWORD Index)
{
LPSOCKET_INFORMATION SI = SocketList[Index];
DWORD i;

closesocket(SI->Socket);

cout<<"Closing socket\n";

GlobalFree(SI);

// Remove from the socket array
for (i = Index; i < TotalSockets; i++)
{
SocketList[i] = SocketList[i + 1];
}

TotalSockets--;
}
Jun 11 '07 #3
Har
3
Could you please help me to solve the problem.Since im new to this type of socket programming i have been in desperate situation. im going to develop a simulator to test the remote devices in the same machine.i think the first step to develop is to connect both the client and server in the same machine through the socket programming but im not able to connect it.both the client and server application runs without any errors but still while running it shows connect error which i have mentioned in my previous post along with the source code.
is there any configuration needed in the system?
Since Im beginner to the socket programming please anyone guide me and help me out of the problem.and also guide me in the simulator program for remote devices.
Jun 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ophir | last post by:
Hello all ! I wrote a simple ActiveX DLL to wrap winsock functionality so I can use it in an ASP page. I call it MyWinSock In the Class module I use this declaration: Dim ctlSocket as...
1
by: Chris Thompson | last post by:
I'm trying to mess around with creating a multi-user server but I'm really clueless as to how to start. I don't know what I'd need to do, and more than likely it's over my head, but I'd still like...
3
by: Bill | last post by:
When vb6 Winsock.RemoteHost is set to "127.0.0.1", c# socket listener cannot hear connect request (my old vb6 winsock listener could hear it...). Why doesn't this work, and is there a work...
1
by: Yu Chai | last post by:
Hi guys, I created a ASP page that 1. users can run when WinSock proxy are using (ie's one is unchecked) 2. users can't run when WinSock proxy are using (ie's one is checked) 3. users can't run...
5
by: windandwaves | last post by:
Hi Folk I proudly send my emails using John Rhoton's code for sending them directly via winsock. I now want to take this system to the next level and start sending emails with HTML content. ...
7
by: Nadav | last post by:
Hi I am writing some kind of a storage system that have to deal with large amounts of data passing over the net, Now, I Wonder... traditional programming would use win32 Winsock DLL as the means...
5
by: kc | last post by:
Hi Just upgrading a app from VB6 to VB.Net. All is going well apart from the Winsock control. The first thing we notice is that there does not appear to be a .Net version (please correct me if...
1
by: Nicolas Ghesquiere | last post by:
Hello I have a problem with my current program. The meaning of the program is to allow users to login to a server to allow them to access the internet. My program communicates with a MS isa...
2
by: Rich | last post by:
I have some old C code that uses winsock for network programming. It appears that VC++ 2005 (Express) does not include winsock support. Is there any way to use winsock functions?
3
AaronL
by: AaronL | last post by:
Hello, I am currently working on a project that has me in sort of a bind. What I want to do is retrieve web pages from the internet, and strip them down to just text. I'll get using Regular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.