473,569 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need sockets help, stuck on problem

175 New Member
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. #include<winsock2.h>
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. #pragma comment(lib, "ws2_32.lib")
  8. #define STRING_MAX 2048
  9. #define MAX 640000
  10. #define MAX_CON 16
  11.  
  12. int nret;
  13. SOCKET MySock;
  14.  
  15. char *client_send(char *targetip, int port)
  16. {
  17.  
  18.   WSADATA wsaData;
  19.   WORD wVersionRequested;
  20.   wVersionRequested = MAKEWORD(2, 2);
  21.   if(WSAStartup (wVersionRequested, &wsaData) != 0){ }
  22.  
  23.   MySock = socket(AF_INET, SOCK_STREAM, 0);
  24.  
  25.   struct sockaddr_in sock;
  26.  
  27. ////////////////////////////////////////////////////////////////////////  
  28. //hostent --> used by functions to store information about a given host
  29.  
  30.   struct hostent* target_ptr;
  31.   if((target_ptr= gethostbyname(targetip)) == NULL){  }
  32.  
  33.   memcpy(&sock.sin_addr, target_ptr->h_addr, target_ptr->h_length);
  34.   sock.sin_family= AF_INET;
  35.   sock.sin_port=htons(port);
  36. //////////////////////////////////////////////////////////////////////// 
  37.  
  38.   if((connect(MySock, (struct sockaddr *)&sock, sizeof(sock) ))){  }
  39.  
  40. //connect --> establishes a connection to a specified socket
  41. //MySock --> an unconnected socket
  42. //(struct sockaddr *)&sock -- Name of the socket in the sockaddr structure to which 
  43. //                              the connection should be established
  44. //sizeof(sock) -- Length of name, in bytes
  45.  
  46.   char *recvString= new char[MAX];
  47.   nret = recv(MySock, recvString, MAX + 1, 0);
  48.  
  49.  
  50. while(nret < 1)
  51. {
  52.   nret = recv(MySock, recvString, MAX + 1, 0);
  53. //  printf("\n %d bytes received", nret);
  54. }
  55.  
  56. //recv --> receives data from a connected or bound socket
  57. //MySock -- identifies a connected socket
  58. //recvString -- buffer for incoming data
  59. //MAX + 1 -- The length, in bytes, of the buf parameter
  60. //0 -- pointer flag
  61.  
  62.   char *output= new char[nret];
  63.   strcpy(output, " ");
  64.   strncat(output, recvString, nret);
  65.   delete [ ] recvString;
  66.  
  67.   closesocket(MySock);
  68.  
  69.   WSACleanup();
  70.  
  71.   return (output);
  72.  
  73.   delete[ ] output;
  74. }
  75.  
  76.  
  77. int main(int argc, char *argv[])
  78. {
  79.     int port = 888;
  80.     char* targetip;  
  81.     char* output = NULL;
  82.  
  83.     targetip =  argv[1];
  84.  
  85.     while(1)
  86.     {
  87.         printf("--> ");    
  88.         printf("%s \n", client_send(targetip, port));
  89.     }
  90. }
  91.  
  92.  
  93.  
Sep 13 '07 #1
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
Sep 14 '07 #2
manontheedge
175 New Member
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.
Sep 14 '07 #3

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

Similar topics

0
1854
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,
1
3770
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...
0
1801
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...
0
1258
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...
1
1368
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...
1
1295
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...
2
2340
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...
0
1433
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....
0
7698
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...
0
7612
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...
0
7924
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, 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. ...
0
8122
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...
1
7673
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...
0
6284
agi2029
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...
1
5513
isladogs
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...
0
5219
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...
0
937
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...

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.