Is it possible to connect to a socket, first send a request and receive a response then send another request and receive its response within the same socket connection ?
I want to eliminate the costs of creating new socket connection for each request.
A TCP connection is an infinite bidirectional stream of bytes. There are no messages/requests at the TCP level. You can send and receive as many bytes as you want. This is even required by many application protocols.
Related
I am trying to send a tcp message to a gprs server. I was trying to do it through WCF, however, I always see tutorials or information regarding the consumption of services, however, there is no service to consume. It is simply sending a tcp-type message to a server using WCF. I have the port, and the server for the connection. How could I do it? I need to create the WCF client for sending messages, but I don't know how without services.
I am using visual studio and c # language
I am trying to send a tcp message to a gprs server. I was trying to do it through WCF
Since the remote server is not a SOAP service but simply a server exposing a TCP port you should use TCP APIs rather than WCF. The reason being that message content and protocol will be different. WCF uses SOAP messages which generally are sent as XML text as opposed to TCP which is binary. Now you could configure WCF to serialize as binary but the result will still be different. There are alot more differences to TCP vs WCF which I won't go into here.
Use the following TCP example:
TcpClient client = new TcpClient("localhost", 80); //enter your server ip and port
// Translate the passed message into ASCII and store it as a Byte array
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get the stream for writing
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
//Close everything to avoid memory leaks
stream.Close();
client.Close();
I created a system to connect a series of TcpClient to a TcpListener and exchange data in a chat-like system. Once the connection is established, the server adds the client to a List, and starts reading the stream waiting for messages.
Once the server has received a message, is there a build-in method to know which client sent it?
Alternative: I thought of attaching the client's RemoteEndPoint (Ip + port) to the message to use it as an identifier, which should be the same between the two versions of the TcpClient on the client and on the server, and unique compared to the other clients. Am I right?
Once the server has received a message, is there a build-in method to know which client sent it?
Anything there is for you to add; typically you would maintain some nominal per-connection state alongside each Socket / TcpClient / TcpListener / NetworkStream / Pipe (your choice of API) instance, so that when receiving a message you can trivially look up whatever you need. In some cases, it may be succifient to just use the Socket / TcpClient / etc instance as a key, but more often you'll have some kind of user-state/context information. This is basically entire up to you to implement.
I'm having a problem receiving data from a client using .net sockets. The client is communicating with TCP and sending one or two packets before closing the connection immediatly. The reception go like this :
Socket newConnection = listener.EndAccept(ar);
newConnection.BeginReceive(myBuffer,0, length, SocketFlags.None, Callback,null);
and the execution won't go further. The beginReceive will immediatly throw a SocketException saying the connection was reset. I can't manage to get the received data.
Here is what I see for this connection on wireshark :
SYN // SYN, AKC // AKC
PSH, ACK for 156 bytes
PSH, ACK for 176 bytes
RST, ACK
And that's it. Is there any means to get this received data even though the connection is closed by the client?
First of all with this type of communication it might be a good idea to switch to UDP instead of TCP - that way you will be able to get the packets regardless of the state of the sender.
Assuming TCP is the only option - I would suggest using synchronous calls:
Socket newConnection = listener.AcceptSocket();
newConnection.Receive(myBuffer, 0, length, SocketFlags.None);
I am new in C#. I made a very simple TCP server and TCP Client. I am able to send some message from client to server. If I want see the message which came from client on server I am using button which view the message. Now my stupid question. How make a function which will react on new comming packet from client to view it imediately in textBox? Simple what I want>>> IF comes a new packet......DO SOMETHING.
Generally, a TCP server does this:
Create a thread to listen for connection requests
Do a TcpListener.AcceptTcpClient in the above thread
When AcceptTcpClient accepts a connection, create a new thread
In new thread, do a GetStream and then Read the stream.
When data arrives, decode it and send a message to the GUI / Controller / whatever.
Process the TCP message and send a response message to the TCP thread to Write on the stream the result of the processing.
I'm writing a simple proxy (more a packet logger) for an online game in C#. All the packets get received by the proxy but some aren't sent to the client (not sure about the server).
For example:
Client->Server: Login Packet - My proxy receives the packet, displays it and sends it to the server.
Server->Client: Connected! Packet - My proxy again receives the packet, it also displays it and sends it to the client.
Server->Client: Chat channels packet - My proxy again receives the packet, it also displays it but the client doesn't receive it. There is no exception.
My code: http://lesderid.pastebin.com/Km7vT2jF
(This is the same project as here: Why can't I send to the listening socket anymore?)
This is just from a brief reading of the code:
Do not bind to 127.0.0.1. Bind to IPAddress.Any instead.
OnDataReceivedFromServer needs to call EndReceive.
I don't recommend mixing synchronous (Send) and asynchronous (BeginReceive) operations on the same socket.