473,695 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Winsock programming

3 New Member
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 6617
Banfa
9,065 Recognized Expert Moderator Expert
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 New Member
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(M AKEWORD(2,0),&w saData) != 0)
{
cout<<"Socket Initialization Error. Program aborted\n";
return;
}
lhSocket = socket(AF_INET, SOCK_STREAM,IPP ROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
cout<<"Invalid Socket "<<GetLastError ()<<". Program Aborted\n"<<end l;
}
memset(&lSockAd dr,0, sizeof(lSockAdd r));
lSockAddr.sin_f amily = AF_INET;
lSockAddr.sin_p ort = htons(gPort);
lSockAddr.sin_a ddr.s_addr = inet_addr("127. 0.0.1");
lConnect = connect(lhSocke t,(SOCKADDR *)&lSockAddr,si zeof(SOCKADDR_I N));
if(lConnect != 0)
{
cout<<"Connect Error. Program aborted\n";
return;
}
lLength = send(lhSocket,l Data,strlen(lDa ta),0);
if(lLength < strlen(lData))
{
cout<<"Send Error.\n";
}
closesocket(lhS ocket);
return;
}


Server:

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

#define PORT 1250
#define BUFFERSIZE 8192

typedef struct _MYSOCKET_INFOR MATION {
CHAR Buffer[BUFFERSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD SendBytes;
DWORD RecvBytes;
} SOCKET_INFORMAT ION, * LPSOCKET_INFORM ATION;

BOOL CreateSocketInf ormation(SOCKET s);
void FreeSocketInfor mation(DWORD Index);

DWORD TotalSockets = 0;
LPSOCKET_INFORM ATION 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(MAKE WORD(2,0),&wsaD ata)) != 0)
{
cout<<"Start up failed";
}

// Create a socket for the winsock tutorial.

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

InternetAddr.si n_family = AF_INET;
InternetAddr.si n_addr.s_addr = htonl(INADDR_AN Y);
InternetAddr.si n_port = htons(PORT);

if (bind(ListenSoc ket, (SOCKADDR *) &InternetAdd r, sizeof(Internet Addr))
== SOCKET_ERROR)
{
cout<<"Winsock tutorial error: Binding failed \n"<<WSAGetLast Error();
return;
}

if (listen(ListenS ocket, 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(Li stenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
cout<<"ioctlsoc ket() failed \n";
return;
}

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

// Check for connection attempts.
FD_SET(ListenSo cket, &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(SocketLi st[i]->Socket, &Writer);
else
FD_SET(SocketLi st[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(Liste nSocket, &Reader))
{
Total--;
if ((AcceptSocket = accept(ListenSo cket, 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(Ac ceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
cout<<"Winsock tutorial error: ioctlsocket() failed with error \n", WSAGetLastError ();
return;
}

if (CreateSocketIn formation(Accep tSocket) == FALSE)
return;
}
else
{
if (WSAGetLastErro r() != 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_INFORM ATION 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(Socke tInfo->Socket, &Reader))
{
Total--;

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

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

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

// If zero bytes are received, this indicates connection is closed.
if (RecvBytes == 0)
{
FreeSocketInfor mation(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(Socke tInfo->Socket, &Writer))
{
Total--;

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

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

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

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

BOOL CreateSocketInf ormation(SOCKET s)
{
LPSOCKET_INFORM ATION SI;

cout<<"Accepted socket\n";

if ((SI = (LPSOCKET_INFOR MATION) GlobalAlloc(GPT R,
sizeof(SOCKET_I NFORMATION))) == 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 FreeSocketInfor mation(DWORD Index)
{
LPSOCKET_INFORM ATION 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 New Member
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
9337
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 Variant.
1
4551
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 to mess around. If anybody could point me in the right direction it would be greatly appreciated.
3
694
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 around I can make on the C# side to hear the connect request? -Bill (don't reply by e-mail, the address is a fake) ______________________________ Steps to reproduce: Start the C# Listener
1
2534
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 when WinSock proxy are not using (ie's one is checked) 4. users can't run when WinSock proxy are not using (ie's one is unchecked) So, what is the difference between winsock proxy client and the proxy setting in browser (IE)?
5
2998
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. Does anyone have any experience in this area? Thank you
7
2850
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 of data transportation... now, indigo is the new communication layer of the CLR, - Does indigo uses Winsock internally? - Is it possible to use indigo for such a task? - I assume using indigo has it's performance penalty ( doesn't it ? - Does...
5
6898
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 wrong). Also when we use the VB6 winsock control in a dotnet version we appear to lose the ability of creating an array on winsock's. for example we cannot assign as thus
1
5429
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 server to make a rule for that IP for the time-credits left for that user. My problem is that so far, the program can handle everything except more then one client, and i have done my best, but i can't find a decent solution
2
1939
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
7609
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 Expressions to strip out the HTML code itself, the problem is actually getting the web pages from the internet. I tried using the Microsoft Internet Transfer Control but my client was experiencing problems with some web pages not downloading, this...
0
8647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9004
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.