I've got two separate programs, one being the "server" and the other one (this one) being the "client". I've got the "server" to where it just sits and waits for connections, and sends out whatever was typed it. My problem is that this "client" code receives ONE message for the "server" and won't receive any more than that. I'm lost at this point. I've overcommented the code intentionally and took out all the error codes, because I wasn't getting any...there's no error, it's just not receiving anything more. If anyone can help me out, or tell me where I've gone wrong I would really appreciate it. -
-
#include<stdio.h>
-
#include<winsock2.h>
-
#include<iostream>
-
using namespace std;
-
-
#pragma comment(lib, "ws2_32.lib")
-
#define STRING_MAX 2048
-
#define MAX 640000
-
#define MAX_CON 16
-
-
int nret;
-
SOCKET MySock;
-
-
char *client_send(char *targetip, int port)
-
{
-
-
WSADATA wsaData;
-
WORD wVersionRequested;
-
wVersionRequested = MAKEWORD(2, 2);
-
if(WSAStartup (wVersionRequested, &wsaData) != 0){ }
-
-
MySock = socket(AF_INET, SOCK_STREAM, 0);
-
-
struct sockaddr_in sock;
-
-
////////////////////////////////////////////////////////////////////////
-
//hostent --> used by functions to store information about a given host
-
-
struct hostent* target_ptr;
-
if((target_ptr= gethostbyname(targetip)) == NULL){ }
-
-
memcpy(&sock.sin_addr, target_ptr->h_addr, target_ptr->h_length);
-
sock.sin_family= AF_INET;
-
sock.sin_port=htons(port);
-
////////////////////////////////////////////////////////////////////////
-
-
if((connect(MySock, (struct sockaddr *)&sock, sizeof(sock) ))){ }
-
-
//connect --> establishes a connection to a specified socket
-
//MySock --> an unconnected socket
-
//(struct sockaddr *)&sock -- Name of the socket in the sockaddr structure to which
-
// the connection should be established
-
//sizeof(sock) -- Length of name, in bytes
-
-
char *recvString= new char[MAX];
-
nret = recv(MySock, recvString, MAX + 1, 0);
-
-
-
while(nret < 1)
-
{
-
nret = recv(MySock, recvString, MAX + 1, 0);
-
// printf("\n %d bytes received", nret);
-
}
-
-
//recv --> receives data from a connected or bound socket
-
//MySock -- identifies a connected socket
-
//recvString -- buffer for incoming data
-
//MAX + 1 -- The length, in bytes, of the buf parameter
-
//0 -- pointer flag
-
-
char *output= new char[nret];
-
strcpy(output, " ");
-
strncat(output, recvString, nret);
-
delete [ ] recvString;
-
-
closesocket(MySock);
-
-
WSACleanup();
-
-
return (output);
-
-
delete[ ] output;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
int port = 888;
-
char* targetip;
-
char* output = NULL;
-
-
targetip = argv[1];
-
-
while(1)
-
{
-
printf("--> ");
-
printf("%s \n", client_send(targetip, port));
-
}
-
}
-
-
-
2 1457 gpraghuram 1,275
Recognized Expert Top Contributor
Hi,
Before doing a recv call do a select and check whether there is any data available on the port for reading.
Also check whether the server is not closing the connection after sending a message to the client
Raghuram
thank you for the reply. I actually found the problem, I have some "server" and this "client" code...
I was just plane doing some things wrong. I had the server listening for connection attempts and then sending out THE message to the client. I realised my mistake, and that the client is the one that I wanted to send the message to the server. Basically, I had enough stuff backwords to make it not work right. I thought I had pinpointed the problem to the "client" code when I posted. When I realised my mistake, it only took about 30 minutes to get it running correctly.
anyway, not sure if that's gonna be clear, but thank you for the help, and, it's working fine now.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Molly |
last post by:
Peace, Pythonphiles!
Python's std lib BaseHTTPServer: I'm stuck with a nasty spoiler -- I
get erratic "Network error: disconnected by peer" for _POST_ requests
only.
=Background=
I'm developing a Python CGI app. Was using Xitami, which is really
light and nice, reasonable performance (circa 200-300 msec to respond,
|
by: Dmitry Akselrod |
last post by:
Hello everyone,
I have a vb.net application that wraps the TCPListener object
in a class. The server connects to the local interface and establishes
itself on port 9900. It then polls for pending connections every 500ms.
I also have a vb6 application that uses the WinSock control at the other end
of the communication tunel. I have to...
|
by: mrpolitics |
last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd)
and everything was fine untill yesterday when the server crashed. So I did
a cold restart and staretd the server back up now it's throwing this stream
of errors right away. Does anyone have any idea what they mean. I havn't
changed the source at all since it was...
|
by: raza |
last post by:
Hi,
I have been programmin in C# for quite some time now and recently got
into sockets programming in C# though i have done it in C++ on
windows/linux . I have an interesting problem at hand .
I am porting a C++ tunnel library into C# . It has 2 functions
WriteOut: I make a header and send it then send the actual data to the
server and...
|
by: raza |
last post by:
Hi,
I am using C# sockets and sending data packets of 25k per packet in
busy loop and receiving data on the same sockets in another thread. I
use select to check avaiability of data.
The problem is that after a while (variable amount of time) the
Socket.Send call hangs , hangs mean it never returns neither throws
exception making my app...
| |
by: taras |
last post by:
IE 6.0 sockets number (TCP/IP channels number) for the same Site ???
I've discovered the restriction: IE 6.0 has 2 sockets only to the same
Server (ASP.NET Site).
Where can I find any kind of information about this limitation?
P.S.
IE 6.0 from Windows 2003 Server has the same limitation,
but if the sockets number is increased to 3, then...
|
by: Stressed Out Developer |
last post by:
We have an application that has a 200 count loop that does the following:
' Each time thru the loop we pass the next IP Address is a range (aka
192.168.4.50 thru 192.168.4.254)
Try
If mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr...
|
by: rossabri |
last post by:
This topic has been addressed in limited detail in other threads:
"sockets don't play nice with new style classes :(" May 14 2005.
http://groups.google.com/group/comp.lang.python/browse_thread/thread/76d27388b0d286fa/c9849013e37c995b
"Subclassing socket" Dec 20 2005 - Jan 14 2006....
|
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...
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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...
|
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...
| | |