473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

nothing is returning from remote application.... socket problem

I am using the following code, don't know what am i missing. in BYTE2 I
am not getting any information from the remote application.
any guidance would be highly appreciated, thanking you in advance

// PatentRequestPaket is a structure defined above in the class
//public struct PatentRequestPacket
//{
// public char[] Key;// = new char[15];
// public char[] PatentNumber;// = new char[20];
// public int NumPages;
//}

PatentRequestPacket packet = new PatentRequestPacket();
PatentRequestPacket ackPacket = new PatentRequestPacket();

int m_nPort = 50055;
Socket ConnectSocket = new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

TcpClient socketForServer;
try
{
socketForServer = new TcpClient("127.0.0.1", 50055);
}
catch
{
Console.WriteLine(
"Failed to connect to server at {0}:999", "localhost");
return;
}

IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"),
50055);
ConnectSocket.Connect(ipEnd);
NetworkStream networkStream = socketForServer.GetStream();

int bytesSent;
int m_nNumPages = -1;
packet.Key = m_strKey.ToCharArray();
packet.PatentNumber= "6655414".ToCharArray();
packet.NumPages = m_nNumPages;
byte[] BYTE = new byte[10000];
BYTE = RawSerialize(ackPacket);
byte[] BYTE2 = {Convert.ToByte('0')};
BYTE2= RawSerialize(packet);

ConnectSocket.Send(BYTE);
ConnectSocket.Receive(BYTE2);

// Rawserialize is a method defined in the class
// public static byte[] RawSerialize( object anything )
// {
// int rawsize = Marshal.SizeOf( anything );
// IntPtr buffer = Marshal.AllocHGlobal( rawsize );
// Marshal.StructureToPtr( anything, buffer, false );
// byte[] rawdatas = new byte[ rawsize ];
// Marshal.Copy( buffer, rawdatas, 0, rawsize );
// Marshal.FreeHGlobal( buffer );
// return rawdatas;
// }

Jan 31 '06 #1
6 2224
Apart from any other problem you should always check the return from
receive:

"If you are using a connection-oriented Socket, the Receive method will read
as much data as is available, up to the size of the buffer. If the remote
host shuts down the Socket connection with the Shutdown method, and all
available data has been received, the Receive method will complete
immediately and return zero bytes."

Jan 31 '06 #2
Hi,

byte[] BYTE = new byte[10000]; BYTE = RawSerialize(ackPacket);


Why you do this ? you create an array of 1000 bytes and in the next line you
just make it inaccesible?

byte[] BYTE2 = {Convert.ToByte('0')};
BYTE2= RawSerialize(packet);
The same thing, why you assign BTE2 like that?
ConnectSocket.Send(BYTE);
ConnectSocket.Receive(BYTE2);


Are you sending and receiving in the same endpoint ?

You are supposted to send from socketForServer and receive in ConnectSocket

Also I do not like the weay you convert your struct to byte[] you should
not have to p/invoke nothign for this.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 31 '06 #3
Mr. Ignacio Machin, thank you very much for your reply. actually i m
really not getting through with this thing... i have no previous
experience with sockets. I would appreciate if you could guide me more
in this respect..... following is the section of code which i am trying
to translate in C#

void CBandToolBarCtrl::SendMessage(CString message)

{
PatentRequestPacket packet, ackPacket;

int m_nPort = 50055;
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
MessageBox("Error at WSAStartup()");

//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
MessageBox("Failed to create socket");
//printf("Error at socket(): %ld\n",
WSAGetLastError());
WSACleanup();
return;
}

//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in clientService;

clientService.sin_family = AF_INET;

clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );

clientService.sin_port = htons( m_nPort );

//----------------------
// Connect to server.
int status;

for(int nAttempt=1; nAttempt<=5; nAttempt++)
{
Sleep(100*nAttempt);
status = connect( ConnectSocket, (SOCKADDR*)
&clientService, sizeof(clientService) );

if(status != SOCKET_ERROR)
break;
}

if(status == SOCKET_ERROR)
{
MessageBox("Failed to connect to Patent
Hunter");
WSACleanup();
return;
}

//----------------------
// Declare and initialize variables.
int bytesSent;
int bytesRecv = SOCKET_ERROR;

strcpy(packet.Key,m_strKey);
strcpy(packet.PatentNumber,message);
packet.NumPages = m_nNumPages;

//----------------------
// Send and receive data.
for(nAttempt=1; nAttempt<5; nAttempt++)
{
Sleep(100*nAttempt);
bytesRecv = recv( ConnectSocket, (char*)
&ackPacket, sizeof(ackPacket), 0);
if(bytesRecv)
{
//CString str;
//str.Format("Attempt:%d Bytes:%d
packetsize:%d",nAttempt,bytesRecv,sizeof(packet));
//MessageBox(str);
break;
}

}

if(bytesRecv==0)

{
MessageBox("Couldn't receive the connection
acknowledgement from the Patent Hunter");
return;
}

bytesSent = send( ConnectSocket, (char*) &packet,
sizeof(packet), 0 );

//MessageBox("data sent");
//printf( "Bytes Sent: %ld\n", bytesSent );
WSACleanup();

}

i have searched diffrent forums and have gathered that C# code which
you have seen... socketForServer belongs to TcpClient class and there
is no method to send in it.

Jan 31 '06 #4
Yes i have to communicate with an application (Same endpoint i guess)
whose call is made in the above VC code.. kindly help

Jan 31 '06 #5

"Khadim" <as***********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

[lots of stuff]

khadim - I can't be bothered to check all your code but I will tell you one
tip for TCP usage that you haven't covered:

There is (in general) no connection between the number of bytes in a send
and the number of bytes in a receive - The implementation and the network
are entitled to chop it up into any number of chunks each of which may
require a separate call to receive - this is not a C# or .NET thing it is
TCP and therefore in any low level socket interface.

consequently all low level receive routines have to accumulate bytes into a
buffer with repeated receives until they know that they have a complete
message or that the other end has closed (usualy indicated by a receive of 0
bytes).

with the async routines this gets quite messy so i tend to avoid them and
use a separate thread and the synchronous methods.

Usually what happens is that people just send small messages which means
that they will usually get away with it until they increase their package
size to greater than one ethernet payload and their app fails.

Jan 31 '06 #6
Hi,

"Khadim" <as***********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Mr. Ignacio Machin, thank you very much for your reply. actually i m
really not getting through with this thing... i have no previous
experience with sockets. I would appreciate if you could guide me more
in this respect..... following is the section of code which i am trying
to translate in C#


Does this C++ code works?

As another poster said, I cannot check in details your code

I noted a couple of erros in your C# code, basically that you are
sending/receiving using the same endpoint, did u changed it?
Why you need to translate the code in the first place? just create your own.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Graham | last post by:
Can anyone tell me how to detect when a client socket connection has failed (ie remote equipment power failure) using VB.net. Thanks in advance
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
0
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished...
4
by: Vicente García | last post by:
Hello!! sorry for my english, I've made an application that ran correctly for 3 days, It's a service, however in the third day the application failed because of "An existing connection was...
4
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
4
by: Anbu | last post by:
Hi All, I'm using the Cassini component in my desktop application to create a web site to host the web services. The application works fine in normal scenario. If the system is kept idle for...
3
by: Wayne And Miles | last post by:
I have created a server application that listens for connections using the TCPListener class. When I connect to the server using a client on the same machine as the server, all works as expected. ...
10
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out...
7
by: Guy Davidson | last post by:
Hi Folks, I'm having some issues with an small socket based server I'm writing, and I was hoping I could get some help. My code (attached below) us supposed to read an HTTP Post message...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.