I have a web service on a server in my company that we have restricted access to from all but one other server on our network.
I do however need to make calls to this from another machine. Is there a way I can spoof the other servers IP address in order to send an http request to the web service? I only need to send it info I don't need any returned data. It's for logging hits from another server on our main server.
I am using this
IPEndPoint endpointAddress = new IPEndPoint(IPAddress.Parse(ipAddress), 80);
using (Socket socket = new Socket(endpointAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
socket.SendTimeout = 500;
socket.Connect(endpointAddress);
socket.Send(byteGetString, byteGetString.Length, 0);
}
but get an exception
A connection attempt failed because
the connected party did not properly
respond after a period of time, or
established connection failed because
connected host has failed to respond
23.202.147.163:80
In general, it is not possible to establish a TCP connection with a server without being able to receive and process some reply packets from that server. HTTP is built upon TCP, and TCP starts communications with a "3-way handshake" that lets the client and server communicate.
The start of an HTTP request is not a single packet.
You could use a proxy to bounce your requests from an IP address that has access.
Related
Server.cs - https://hastebin.com/enajinewij.cs
Client.cs - https://hastebin.com/iriperubur.cs
I have tried both running the Client on another PC and running it on one PC, but both result in the Client not being able to receive or send any messages.
I CANT portforward. I am using Hamachi for the IP Address. Both client and server are connected to my Network and are using the Hamachi IP Address. I am using PDA Net to connect to the internet from my PC.
The Server does not see them connect at all. Nor does the Server get any messages from them. Currently only the Server can send messages, and only it can get them.
I am not getting ANY errors at all, so I am not sure how I should handle solving this issue as it's my first time working with networking.
At first you create a TcpListener and you call StartLis() that does BeginAcceptTcpClient. However in AcceptTCPClient you create a new TcpListener and BeginAcceptTcpClient is not called.
You don't have to create a new listener for each connection, but you do have to call BeginAcceptTcpclient again:
private void AcceptTCPClient(IAsyncResult ar)
{
TcpListener Lis = (TcpListener)ar.AsyncState;
Clients.Add(new ServerClient(Lis.EndAcceptTcpClient(ar)));
StartLis(); // this will call BeginAcceptTcpClient again
}
Im making a program in c#, the server side program listens to a port and the client connect to the server. But I want to pick which client to connect to when all the clients are trying to connect.
Each connection that's made to your server gives you a reference to a Socket object that you can use for sending data back or serving your clients, e.g.:
Socket handler = listener.Accept()
// ...
// do your processing
// ...
byte[] msg = Encoding.ASCII.GetBytes("Sending response to client");
handler.Send(msg);
Don't lose the reference to that Socket object.
I'm using NetworkComms.Net and I'm currently trying to create a sort of forwarder which listens on a port (calling it GATEWAY), which receive a certain packet (from CLIENT) which will tell him where to redirect next pacekts coming from the same client.
Example:
CLIENT tell GATEWAY that he needs to get to SERVER:serverport
GATEWAY creates a connection to SERVER:serverport
CLIENT sends packets to GATEWAY which sends them to SERVER
SERVER sends back a response, which goes through the GATEWAY to the CLIENT.
I had it working with Net.Sockets and I’m now changing to NetworkComms.
The problem I’m facing is that when a connection from Gateway (client) to Server (listening server) is closed, the Gateway trigger the global callbacks for connection/disconnection:
NetworkComms.AppendGlobalConnectionCloseHandler(ClientDisconnected);
NetworkComms.AppendGlobalConnectionEstablishHandler(ClientConnected);
Those callbacks are supposed to be only called when a client connect or disconnect on the Gateway
This is the code i’m currently using to start the listener.
NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions<ProtobufSerializer, , LZMACompressor>();
NetworkComms.DefaultSendReceiveOptions.IncludePacketConstructionTime = NetworkComms.DefaultSendReceiveOptions.ReceiveHandlePriority = QueueItemPriority.AboveNormal;
NetworkComms.AppendGlobalConnectionCloseHandler(SessionClosed);
NetworkComms.AppendGlobalConnectionEstablishHandler(NewSessionConnected);
NetworkComms.AppendGlobalIncomingPacketHandler<string>("ECHO", SocketCommands.HandleIncomingECHOPacket);
Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 10000));
I suppose this is like a “global” way of starting It?
How do I start one then add those Connected/Disconnected handlers to just the listener?
Thanks
I am working with an application that receives a file by a TCP protocol, the application processes the file and then sends it by the same protocol, i am receiving the file without problems, my problem is when i try to send the file, because i need to send the file to another application that is listening a Dynamic port, the code that i am using to send these files is:
internal void Send(byte[] buffer)
{
TcpClient _client = null;
try
{
_client = new TcpClient(RemoteIPaddress, Dynamic_port);
if (_client != null)
{
NetworkStream _clienttStream = _client.GetStream();
_clienttStream.Write(buffer, 0, buffer.Length);
_clienttStream.Flush();
_clienttStream.Close();
_clienttStream = null;
}
}
catch
{
if (_client != null)
{
_client.Close();
_client = null;
}
}
}
The question is, how can i send a file by TCP protocol to a remote machine that uses a dynamic port
Typically, the server should listen on a well known port for a connection request. The response should include the port number that the server will communicate further on. Then your app connects to that port for transferring the data.
The communication should do the following:
Client connects to server on well known port.
Server responds with the dynamic port number to use for further communication.
Client connects to server on the received port number.
Server responds stating connection established.
Client transmits data and disconnects.
This is a simplified version of how passive FTP works.
Point is, there are only two ways to connect to a server on a dynamic port. The first way is outlined above. If you can't do it that way then your client app will have to do a port scan, sending a connection attempt to every port within a range, and see which one the server responds on. However, firewalls are generally programmed to notice this type of thing and shut you down (it's hacker behavior).
Are you asking how you can determine the dynamic port that the remote machine has selected to use? There is no automated way to do this. The server should either work on a port that both machines are aware of or you should work out a way for them to select a port through some other mode of communication. Either by connecting to a 3rd party server or hosting a web service that the client can access.
I am trying to find the ip address (not the hostname) that responded to my WebRequest in C#. I do not want to do a DNS resolution, because their are cases where the DNS records returned are not the servers responding to the request. For ex:
Client -> Load Balancer -> Web Server
The DNS server would respond with the Load Balancer's IP. Assuming the responding Web server is not going back through the Load Balancer, the IP address would then be the actual Web server which is what I am trying to find.
Do you have access to the server side code? Or to the web server configuration? You could always place the machines IP, or whatever identifier you'd like, in a custom header and look for that on the client.
As for your original question, I do not believe that information is exposed anywhere by the HttpWebRequest/HttpWebResponse classes.
I think you'll have to go OSI-dipping, and create and harness your own socket;
then you'll have access to the RemoteEndPoint property (at least after your socket has connected, or been connected to) like so :
Socket sprocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sprocket.Connect("www.google.com", 80);
string IPAddressOfRespondingServer = ((IPEndPoint)sprocket.RemoteEndPoint).Address.ToString();