I'm writing a socket program which sends a structure from one machine to another. When I run my client first time it runs well, however after the first time I couldn't receive all the data inside the structure (it is like, half of the array is received and the other half is not set). I think it is related with the socket's receive buffer size or the amount of data that I can send through the network (I'm not sure though, but sender side returns the correct byte size as sent data). Do you have any idea what I'm missing? Is there any information that I need to consider while sending my data such as maximum size or something else?
Below is my code:
config.h
Code:
#define TOLDATA 512*2 //Maximum data elements that are going to be sent through network
/* States of the protocol.
* At the moment they are only:
*
* NOOP: No operation
* TRANSMIT : During data transmission
* COMPLETE : Data transmission complete
*/
#define TRANSMIT 0x01
#define COMPLETE 0x02
/* This is the basic structure that is used for
* transferring data from one place to other
* @param state Current machine's state
* @param dataSize exact number of elements in data array
* @param data data to be transmitted
*
*/
typedef struct Tol_Data{
char state;
int dataSize;
double data[TOLDATA];
} TolData;
#define RCVBUFSIZE sizeof(TolData) //Maximum bytes that can be received
client.cpp
Code:
#include "PracticalSocket.h" // For Socket and SocketException
#include <iostream> // For cerr and cout
#include <cstdlib> // For atoi()
#include "config.h"
using namespace std;
TolData* getDumbData(){
TolData* data = new TolData();
for(int i=0;i<TOLDATA;i++)
data->data[i]=TOLDATA-i;
data->dataSize=TOLDATA;
data->state=TRANSMIT;
return data;
}
int main(int argc, char *argv[]) {
if ((argc < 2) || (argc > 4)) { // Test for correct number of arguments
cerr << "Usage: " << argv[0]
<< " <Server> " << endl;
exit(1);
}
string servAddress = argv[1]; // First arg: server address
TolData* data = getDumbData();
int dataSize = sizeof(*data);// Determine input length
unsigned short echoServPort = 20000;
try {
// Establish connection with the server
TCPSocket sock(servAddress, echoServPort);
//Send data
int snt=sock.send(data, dataSize);
cout << "data has been sent: " << snt << endl;
} catch(SocketException &e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}
server.cpp
Code:
#include "PracticalSocket.h" // For Socket, ServerSocket, and SocketException
#include <iostream> // For cerr and cout
#include <cstdlib> // For atoi()
#include <vector>
#include "config.h"
using namespace std;
vector<double> myData;
void HandleTCPClient(TCPSocket *sock, TolData* echoBuffer); // TCP client handling function
int main(int argc, char *argv[]) {
if (argc != 2) { // Test for correct number of arguments
cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
exit(1);
}
unsigned short echoServPort = atoi(argv[1]); // First arg: local port
//Buffer for receive
TolData* echoBuffer=new TolData();
try {
TCPServerSocket servSock(echoServPort); // Server Socket object
for (;;) { // Run forever
HandleTCPClient(servSock.accept(),echoBuffer); // Wait for a client to connect
}
} catch (SocketException &e) {
cerr << e.what() << endl;
exit(1);
}
// NOT REACHED
return 0;
}
//Assuming that this data is double
//later i need to figure out how to extend
//this type definition to template T
void handleData(double *recvData, unsigned int size){
for(int i=0;i<size;i++){
cout << "Data is about to be added: " << *recvData<< endl;
myData.push_back(*(recvData++));
}
}
// TCP client handling function
void HandleTCPClient(TCPSocket *sock, TolData* echoBuffer) {
cout << "Handling client ";
try {
cout << sock->getForeignAddress() << ":";
} catch (SocketException e) {
cerr << "Unable to get foreign address" << endl;
}
try {
cout << sock->getForeignPort();
} catch (SocketException e) {
cerr << "Unable to get foreign port" << endl;
}
cout << endl;
int recvMsgSize;
while ((recvMsgSize = sock->recv(echoBuffer, RCVBUFSIZE)) > 0) {
cout << "data is received size:" <<recvMsgSize<< endl;
TolData *received = echoBuffer;
if(received->state && TRANSMIT)
//Send data and size information to the handle function
handleData(received->data,received->dataSize);
//At the moment COMPLETE state is not being used
if(received->state && COMPLETE)
break;
}
cout << "All data have been received!" << endl;
delete sock;
}
PracticalSocket is a small lib. that I'm using for connection, sending data and receiving it.
After I run server, I execute client several times from terminal and I'm getting the problem after first execution (or sometimes at my first execution). If I change the value in TOLDATA in config.h like <=512, it is working.
The server doesn't know how much data is being sent to it. Is the recv() call blocking or polling? If it's blocking, then you might be okay. If it's polling, you need to distinguish between "End of data" and "no more data YET".
Then there's
Code:
handleData(received->data,received->dataSize);
How does the received object (echoBuffer) know dataSize? Does the recv() call set that too? Or is it set to the maximum buffer size? In any case, it might be more (or less) than recvMsgSize, which would reflect the true number of bytes received.
Hi
The clients connect to my server -using port 9130. But no client could connect to my server at this time. I've checked already and this is the result
netstat -Aan|grep -v 127.0.0.1|grep 9130|pg
f10006000abcb398 tcp4 10313 0 10.0.89.81.9130 10.158.70.24.1705 CLOSE_WAIT... (8 Replies)
I been looking for a good guide or some help on how to install and setup TCP-MUX protocol socket on my Solaris 7 servers.
Can anyone point me in right direction of help me ?
Thanks (5 Replies)
I installed a fresh copy of Solaris 7 and present up my ip and domain for my web services but when I try to connect to it I get the following error;
TCPActiveOpen: connect failed tcp/192.168.1.148/7900: 146 (Connection refused).
the port is open in my router but I don't no were to add it in... (5 Replies)
Hello all,
I have a requirement to read and write to a tcp socket from an HP-UX shell script. I see a /dev/tcp character device on my servers:
crw-rw-rw- 1 root root 72 0x00004f Mar 28 18:37 /dev/tcp
So I believe this is what I should use. The problem is that all the... (2 Replies)
Hello,
Our software is using a TCP socket (AF_INET, SOCK_STREAM) to communicate with an Ethernet device. When we send a message, the message object writes itself in full onto the socket's stream buffer before the software invokes send() from socket.h.
I'm still researching, but have 2... (1 Reply)
Hello,
I'm trying to write a small c application to test a tcp port. This works fine for the most part but the default timeout on the connect is very long. I have been reading many posts but and it looks like I need to set the socket to be non-blocking and poll for a result. I have been totally... (2 Replies)
Hello
I try to send DHCP RENEW packets to the network and receive the responses. I broadcast the packet and I can see that it's successfully sent using Wireshark. But I have difficulties receiving the responses.I use packet sockets to catch the packets. I can see that there are responses to my... (0 Replies)
I have what appears to be a unique socket problem, although admittedly my tcp/ip programming experience is relatively limited.
I have a AIX server process using TCP/IP berkely sockets, and a Windows (C#) process. The windows process takes input from a user and sends a message to the Unix... (1 Reply)
If I do a netstat -a I can see all the sockets currently open, is there a way that I can tell which application is holding open these sockets ? (3 Replies)
Hello there chaps.
First of all, i'm no TCP/IP-wiz, so forgive me if this is a stupid question.
I've been messing around with filetransfer using sockets, and there is one thing that confuses me.
This is how it's set up:
A server app listens on a port for a client connection.
When it... (3 Replies)