I have written code that sends data using TCP on port 30175 on the client side server. The problem I am having is that even though I close the port between each string it is always sending from the same port on the server side causing the client to not recognize it as a new connection or new data. I would like to be able to specify the port i am sending from my server side. The program is written in c#.
The server listening on port 30175 will send data from port 30175. If you want a different port number then you must open a new port on the server and get the client to connect to it.
My suggestion is that you fix the problem that the client is not recognizing the new connection and new data.
First, lets address your issue. You choose which port the server is using when you Bind() to the socket. At this point, you have the option to specify a port. (If you don't then it chooses an available one for you.)
The question I have for you is... if you have your server switches ports all the time... Then how will the client know what port to Connect() to?
A Different Approach
Instead of closing the port between each string as you described before... consider setting up a protocol. For example: Have the client connect, then send the length (int) of the string in ASCII. Then send the ASCII byte[]. After this, repeat the process. (So the server would be ready for another int.)
This way your code isn't spending as much time binding/connecting to sockets.
Related
I need to bridge traffic from one port (say port 3000) and send it to a new port (say port 4000) and have full bidirectional TCP support.
How should I go about solving this? Should I use the socket class or the TCPClient class?
Would this be as simple as sending the stream of data from one TCPClient to another?
Is there anything else I should be considering?
You program needs to act as both a server and a client:
It should act at a server regarding port 3000, where your program receives connections. Each time you receive a new connection you yourself create a new connection to the actual server on port 4000. Keep these two connections (the one initiated on port 3000 and the one you created to port 4000) together, so you know they are a pair. When you receive data on any of these connection, just send it on the the other connection in the pair.
I have a custom TCP Server listening on port 5888(dummy port). The proxy server listens for incoming connections. When the proxy receives HTTP request for certain pages, it should relay it to the main server on port 80.For other page requests the proxy is required to send data to the main Server on port 8081.
The port 80 is used to service the HTML Pages where as the port 8081 is used for streaming data to the clients.
I am able to receive the incoming connections on the proxy and then read the data from the clients. After reading the data, I can determine which port to connect to on the main server for sending the data.
I am stuck at deciding how to connect on 2 ports for sending the data from the clients to the Main Server?
In that case you either need 2 socket connection objects to the same IP on the different ports (this is legal), or you have one connection object which reconnects according to the port you have to deal with.
Depending on how often you have to switch connections the latter version might have a huge overhead, plus the first one allows you to send data to both ports virtually simultaneously.
You need to stop thinking of your program as a server. After you have received the connection, read the data, and decided what port to send it to, shift gears and start operating as a client would.
Just open a new connection to "localhost" on either port 80 or 8081 and re-send the data you received as if you were the original client.
Your client is connected to the proxy server on port 5888 so no matter from what real server (Web or streaming) you take the data, you are going to provide the data to the client using port 5888 only.
It seems to be a not so practical solution. I am assuming here that you are trying to achieve a kind of control port and data port structure where one port is controlling the streaming from another port.
Just creating two sockets is sufficient for obtaining data from two servers. Here you will have to manually create a protocol which your client understands as you are going to provide both html and streaming data to the client using single port.
i'm using UDP async socket in C#.net and i want to make the server and a client communicate in different port for sending and receive,
the server send to client with port A and receive from client with port B
the client receive from server with port A and send data to server with port B
is it possible??
In the client bind the socket to port A, and in the client bind to port B. It's as simple as that. The server shouldn't really know A, but get it from the messages it receives messages from the client (using something like ReceiveFrom.
Remember that using UDP, the client has to be the first to send messages, otherwise it's not really a client-server system but more distributed system.
This is exactly how it happens already. The source port for a client is a random port chosen by the OS.
It's not possible to do this, since an endpoint consists of only one IP address and one port number. You would need to use two different sockets and establish two connections with the server in order to use port A and port B.
If you were using TCP rather than UDP as part of the constructor of the TCPClient you can specify which EndPoint you want the outgoing connection to use.
I have developed a small server that recives connections (using basically the example here: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx)
The problem is that i dont know how but whenever a connection is made, it cannot send data (or de data sent does not reach the server, the method Read of the NetworkStream from GetStream of the TcpListener just blocks and does not get any data byte.
I tried to just telnet it, instead of using the client i have and the problem is the same. The funny thing is that if i do "telnet localhost 2000" it works perfectly!!
It seems that something is blocking the communications (previously not even localhost was working until i upgraded .NET Fw on the server), but i dont know what can it be that just blocks the data but allows the connections, actually did not know than an application could do it!
Any help with this will be much appreciated!
Thank you!
EDIT:
The line
TcpClient client = server.AcceptTcpClient();
Gets the tcp connection properly then i get the networkstream and when i try to read from it, there is no data received (i == 0):
i = stream.Read(bytes, 0, bytes.Length)
It was a firewall... apparently there is some common service on port 2000 that has a specific protocol, and the firewall blocked all the data that was not following this protocol :/
Even though, thank you for your help!!
Are you trying to connect to your server using a WAN IP instead of a LAN ip?
You may need to forward your router ports to make it work properly.
See this site for help concerning ports forwarding
I am supposed to connect to external server using UDP sockets in C#..
I could not understand these 2 lines in server usage notes:
"Use of dedicated sockets is enforced."
and
"If the server looses UDP connectivity with the client, it will ..."
I thought that the UDP socket is connectionless!
So what did "looses connectivity" mean? and how to avoid it?
Does there is a known way to ensure "dedicated sockets"?
Thanks
"Use of dedicated sockets is
enforced."
To me this says, create one unique socket for each connection and use it throughout that connection.
EDIT: Just to expand on this, from the servers point of view.
UDP sockets are not identified by the
remote address, but only by the local
address, although each message has an
associated remote address. (source).
That way the server can distinguish from which client each message came from. Because the remote address is made up of an ip address and port combination, you should use the same socket throughout your communication of the sever. This is because if you don't, it's possible you could get assigned a different port next time you change the underlying socket.
"If the server looses UDP connectivity
with the client, it will ..."
It is possible to loose UPD connectivity e.g. either of the endpoints in the connection is lost, say I go to the server and pull the plug?
EDIT2:
Dan Bryant makes an excellent point in the comments, that links in with what I was saying about.
One thing worth noting is that it's
possible for a call to a UDP socket to
throw a SocketException with
SocketError.ConnectionReset as the
error code. UDP does not have any sort
of session with structured
connect/disconnect, but it does use a
dynamically-assigned remote port to
allow replies, which is a kind of
'connection'.
After 2 hours trying different -may be random solutions:
The server wants you to introduce yourself on a port other than the one you will use to actually send data. "dedicated sockets"
You know which IP and Port you are sending start info on, but you do not know which will be used for actual data transmission..
Solution
1- You will create your socket -with known IPEndpoint, and send on it the 'start message'..
2- Then wait to receive from Any IP...
3- The server will response 'welcome message', stating the Endpoint it will use.(by changing parameter ref remoteEP of Socket.ReceiveFrom())
4- You must then change the port you are sending on = remote Endpoint port + 1 (why? standard way or something?)
5- At last you can send and receive normally using these ports