Error in casting char* to Object ...

Write your question here.
am writing a protocol using socket programming . when I send segments in the sender , I write

1
2
3
segment seg = window_buffer.dequeue();
buf = (char*) &seg;
i=sendto(client_socket,buf,segment_length,0, (SOCKADDR *) & RecvAddr,sizeof (RecvAddr));


And when I receive in the receiver I write
1
2
3
segment mmm(512);
i = recvfrom(server_socket,buffer,max_seg_length, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
memcpy( &mmm, buffer, sizeof(buffer) );


when debugging , it gives me this error in the receiver :

Unhandled exception at 0x5DD76D26 (msvcp110d.dll) in Networks2.exe: 0xC0000005: Access violation reading location 0x002CFD88
what can I do ?

segment is a class ..

1
2
3
4
5
6
7
8
9
10
11
class segment{
	private:
		char* data;
		_int16 seq_no;
	public:
		segment(int);
		char* getData();
		int getSeqNo();
		void SeqNoE(int);
		void DataE (char*);
};




Thanks in advance .

Your segment class only contains a pointer to the data. You are only sending that pointer, which is useless to the receiver on a completely different machine.
 
buf = (char*) &seg;

You can't cast the &seg to a char pointer (unless you have a cast operator) and expect it to work. You're going to be sending the first segment_length bytes starting at the base of seg, which is not where your data is.

The line should be:
 
buf = seg.getData();


You have a similar problem with the receiver.
 
memcpy( &mmm, buffer, sizeof(buffer) );

That's going to wipe out segment (and data following segment on the stack) and probably cause a segmentation fault. It's not putting data into segment's data area.

Try this instead:
1
2
 
i = recvfrom(server_socket, mmm.getData(),max_seg_length, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);


edit: Not clear where segment_length comes from. It really should be an attribute of segment.
Last edited on
But I need to send the data in the segment and the seq_no too .... the way you told me is only sending the data of the segment .... Is there is another ideas for making a segment with seq_no ?
Try to actually think: why not just send one after the other?
I am using protocol GBN (Go Back N) which is using the sequence no. of the segment and a buffer with a window size ... and the receiver sends Ack when a segment is delivered with its sequence number .... this way checks if segments are lost ... and resend it ...
Topic archived. No new replies allowed.