TCP how do I relay the packets from my client to the server










0















So I am working on creating my own proxy server for my game server.
Whats happening so far is that I try to connect to my Terraria server and it says
Connecting..
Then I start my server application which accepts incoming requests on that specific IP & port and it prompts a MessageBox saying"Connected" and then the game goes from "Connecting..." to "Connecting to server..." but it gets stuck there, this is most likely because I am not redirecting the traffic from my proxy server to my server.. Right?



I've been trying to .Write() to the stream but I think I am writing to the wrong stream, do I write to the stream that accepts connections or do I create a new stream for outgoing traffic?



public partial class MainWindow : Window

public static IPAddress remoteAddress = IPAddress.Parse("127.0.0.1");
public TcpListener remoteServer = new TcpListener(remoteAddress, 7777);
public TcpClient client = default(TcpClient);

public TcpClient RemoteClient = new TcpClient("terraria.novux.ru", 7777);

public MainWindow()

InitializeComponent();


private void BtnListen_OnClick(object sender, RoutedEventArgs e)

if (StartServer())

client = remoteServer.AcceptTcpClient();
MessageBox.Show("Connected");
var receivedBuffer = new byte[1024];

//Should I write to this one instead?
var clientStream = client.GetStream();

var stream = RemoteClient.GetStream();

while (client.Connected)
if (client.Connected)
if (client.ReceiveBufferSize > 0)

receivedBuffer = new byte[1024];
stream.Write(receivedBuffer, 0, receivedBuffer.Length);




private bool StartServer()

try

remoteServer.Start();
MessageBox.Show("Server Started...");
return true;

catch (Exception exception)

MessageBox.Show(exception.ToString());
throw;












share|improve this question






















  • yes, you would need to write to client.GetStream();

    – Anu Viswan
    Nov 15 '18 at 8:03











  • Seems as if that doesnt let me connect still

    – Mark Denom
    Nov 15 '18 at 8:31











  • could u pls share ur client code as well (where client connects to server) ?

    – Anu Viswan
    Nov 15 '18 at 8:45











  • That's all their is, It's listening for a connection on that IP and port, and I've changed it to where when I join my Terraria server, windows redirects the hostname to localhost.

    – Mark Denom
    Nov 15 '18 at 8:50











  • It's a bit more complicated than this. You need one TcpClient that will handle communication (send and receive messages) with the remote server and you will need a TcpListener that will handle communication with the clients. When the TcpListener will receive the message it will forward it to the TcpClient that will send it to the remote server. When the TcpClient receives a message from the remote server it will forward it to the TcpListener that will send it to its connected client.

    – Sergiu Muresan
    Nov 15 '18 at 9:59
















0















So I am working on creating my own proxy server for my game server.
Whats happening so far is that I try to connect to my Terraria server and it says
Connecting..
Then I start my server application which accepts incoming requests on that specific IP & port and it prompts a MessageBox saying"Connected" and then the game goes from "Connecting..." to "Connecting to server..." but it gets stuck there, this is most likely because I am not redirecting the traffic from my proxy server to my server.. Right?



I've been trying to .Write() to the stream but I think I am writing to the wrong stream, do I write to the stream that accepts connections or do I create a new stream for outgoing traffic?



public partial class MainWindow : Window

public static IPAddress remoteAddress = IPAddress.Parse("127.0.0.1");
public TcpListener remoteServer = new TcpListener(remoteAddress, 7777);
public TcpClient client = default(TcpClient);

public TcpClient RemoteClient = new TcpClient("terraria.novux.ru", 7777);

public MainWindow()

InitializeComponent();


private void BtnListen_OnClick(object sender, RoutedEventArgs e)

if (StartServer())

client = remoteServer.AcceptTcpClient();
MessageBox.Show("Connected");
var receivedBuffer = new byte[1024];

//Should I write to this one instead?
var clientStream = client.GetStream();

var stream = RemoteClient.GetStream();

while (client.Connected)
if (client.Connected)
if (client.ReceiveBufferSize > 0)

receivedBuffer = new byte[1024];
stream.Write(receivedBuffer, 0, receivedBuffer.Length);




private bool StartServer()

try

remoteServer.Start();
MessageBox.Show("Server Started...");
return true;

catch (Exception exception)

MessageBox.Show(exception.ToString());
throw;












share|improve this question






















  • yes, you would need to write to client.GetStream();

    – Anu Viswan
    Nov 15 '18 at 8:03











  • Seems as if that doesnt let me connect still

    – Mark Denom
    Nov 15 '18 at 8:31











  • could u pls share ur client code as well (where client connects to server) ?

    – Anu Viswan
    Nov 15 '18 at 8:45











  • That's all their is, It's listening for a connection on that IP and port, and I've changed it to where when I join my Terraria server, windows redirects the hostname to localhost.

    – Mark Denom
    Nov 15 '18 at 8:50











  • It's a bit more complicated than this. You need one TcpClient that will handle communication (send and receive messages) with the remote server and you will need a TcpListener that will handle communication with the clients. When the TcpListener will receive the message it will forward it to the TcpClient that will send it to the remote server. When the TcpClient receives a message from the remote server it will forward it to the TcpListener that will send it to its connected client.

    – Sergiu Muresan
    Nov 15 '18 at 9:59














0












0








0








So I am working on creating my own proxy server for my game server.
Whats happening so far is that I try to connect to my Terraria server and it says
Connecting..
Then I start my server application which accepts incoming requests on that specific IP & port and it prompts a MessageBox saying"Connected" and then the game goes from "Connecting..." to "Connecting to server..." but it gets stuck there, this is most likely because I am not redirecting the traffic from my proxy server to my server.. Right?



I've been trying to .Write() to the stream but I think I am writing to the wrong stream, do I write to the stream that accepts connections or do I create a new stream for outgoing traffic?



public partial class MainWindow : Window

public static IPAddress remoteAddress = IPAddress.Parse("127.0.0.1");
public TcpListener remoteServer = new TcpListener(remoteAddress, 7777);
public TcpClient client = default(TcpClient);

public TcpClient RemoteClient = new TcpClient("terraria.novux.ru", 7777);

public MainWindow()

InitializeComponent();


private void BtnListen_OnClick(object sender, RoutedEventArgs e)

if (StartServer())

client = remoteServer.AcceptTcpClient();
MessageBox.Show("Connected");
var receivedBuffer = new byte[1024];

//Should I write to this one instead?
var clientStream = client.GetStream();

var stream = RemoteClient.GetStream();

while (client.Connected)
if (client.Connected)
if (client.ReceiveBufferSize > 0)

receivedBuffer = new byte[1024];
stream.Write(receivedBuffer, 0, receivedBuffer.Length);




private bool StartServer()

try

remoteServer.Start();
MessageBox.Show("Server Started...");
return true;

catch (Exception exception)

MessageBox.Show(exception.ToString());
throw;












share|improve this question














So I am working on creating my own proxy server for my game server.
Whats happening so far is that I try to connect to my Terraria server and it says
Connecting..
Then I start my server application which accepts incoming requests on that specific IP & port and it prompts a MessageBox saying"Connected" and then the game goes from "Connecting..." to "Connecting to server..." but it gets stuck there, this is most likely because I am not redirecting the traffic from my proxy server to my server.. Right?



I've been trying to .Write() to the stream but I think I am writing to the wrong stream, do I write to the stream that accepts connections or do I create a new stream for outgoing traffic?



public partial class MainWindow : Window

public static IPAddress remoteAddress = IPAddress.Parse("127.0.0.1");
public TcpListener remoteServer = new TcpListener(remoteAddress, 7777);
public TcpClient client = default(TcpClient);

public TcpClient RemoteClient = new TcpClient("terraria.novux.ru", 7777);

public MainWindow()

InitializeComponent();


private void BtnListen_OnClick(object sender, RoutedEventArgs e)

if (StartServer())

client = remoteServer.AcceptTcpClient();
MessageBox.Show("Connected");
var receivedBuffer = new byte[1024];

//Should I write to this one instead?
var clientStream = client.GetStream();

var stream = RemoteClient.GetStream();

while (client.Connected)
if (client.Connected)
if (client.ReceiveBufferSize > 0)

receivedBuffer = new byte[1024];
stream.Write(receivedBuffer, 0, receivedBuffer.Length);




private bool StartServer()

try

remoteServer.Start();
MessageBox.Show("Server Started...");
return true;

catch (Exception exception)

MessageBox.Show(exception.ToString());
throw;









c# .net sockets networking tcp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 3:53









Mark DenomMark Denom

314110




314110












  • yes, you would need to write to client.GetStream();

    – Anu Viswan
    Nov 15 '18 at 8:03











  • Seems as if that doesnt let me connect still

    – Mark Denom
    Nov 15 '18 at 8:31











  • could u pls share ur client code as well (where client connects to server) ?

    – Anu Viswan
    Nov 15 '18 at 8:45











  • That's all their is, It's listening for a connection on that IP and port, and I've changed it to where when I join my Terraria server, windows redirects the hostname to localhost.

    – Mark Denom
    Nov 15 '18 at 8:50











  • It's a bit more complicated than this. You need one TcpClient that will handle communication (send and receive messages) with the remote server and you will need a TcpListener that will handle communication with the clients. When the TcpListener will receive the message it will forward it to the TcpClient that will send it to the remote server. When the TcpClient receives a message from the remote server it will forward it to the TcpListener that will send it to its connected client.

    – Sergiu Muresan
    Nov 15 '18 at 9:59


















  • yes, you would need to write to client.GetStream();

    – Anu Viswan
    Nov 15 '18 at 8:03











  • Seems as if that doesnt let me connect still

    – Mark Denom
    Nov 15 '18 at 8:31











  • could u pls share ur client code as well (where client connects to server) ?

    – Anu Viswan
    Nov 15 '18 at 8:45











  • That's all their is, It's listening for a connection on that IP and port, and I've changed it to where when I join my Terraria server, windows redirects the hostname to localhost.

    – Mark Denom
    Nov 15 '18 at 8:50











  • It's a bit more complicated than this. You need one TcpClient that will handle communication (send and receive messages) with the remote server and you will need a TcpListener that will handle communication with the clients. When the TcpListener will receive the message it will forward it to the TcpClient that will send it to the remote server. When the TcpClient receives a message from the remote server it will forward it to the TcpListener that will send it to its connected client.

    – Sergiu Muresan
    Nov 15 '18 at 9:59

















yes, you would need to write to client.GetStream();

– Anu Viswan
Nov 15 '18 at 8:03





yes, you would need to write to client.GetStream();

– Anu Viswan
Nov 15 '18 at 8:03













Seems as if that doesnt let me connect still

– Mark Denom
Nov 15 '18 at 8:31





Seems as if that doesnt let me connect still

– Mark Denom
Nov 15 '18 at 8:31













could u pls share ur client code as well (where client connects to server) ?

– Anu Viswan
Nov 15 '18 at 8:45





could u pls share ur client code as well (where client connects to server) ?

– Anu Viswan
Nov 15 '18 at 8:45













That's all their is, It's listening for a connection on that IP and port, and I've changed it to where when I join my Terraria server, windows redirects the hostname to localhost.

– Mark Denom
Nov 15 '18 at 8:50





That's all their is, It's listening for a connection on that IP and port, and I've changed it to where when I join my Terraria server, windows redirects the hostname to localhost.

– Mark Denom
Nov 15 '18 at 8:50













It's a bit more complicated than this. You need one TcpClient that will handle communication (send and receive messages) with the remote server and you will need a TcpListener that will handle communication with the clients. When the TcpListener will receive the message it will forward it to the TcpClient that will send it to the remote server. When the TcpClient receives a message from the remote server it will forward it to the TcpListener that will send it to its connected client.

– Sergiu Muresan
Nov 15 '18 at 9:59






It's a bit more complicated than this. You need one TcpClient that will handle communication (send and receive messages) with the remote server and you will need a TcpListener that will handle communication with the clients. When the TcpListener will receive the message it will forward it to the TcpClient that will send it to the remote server. When the TcpClient receives a message from the remote server it will forward it to the TcpListener that will send it to its connected client.

– Sergiu Muresan
Nov 15 '18 at 9:59













1 Answer
1






active

oldest

votes


















1














A simplified implementation could look like this.



public class Program

public static void Main(string args)

StartTcpListener("localhost", 9000);


private static byte SendReceiveRemoteServer(string host, int port, byte data)

try

// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
var client = new TcpClient(host, port);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

var stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent to server: 0", Encoding.ASCII.GetString(data));

// Receive the TcpServer.response.

// Read the first batch of the TcpServer response bytes.
var bytes = new byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


Console.WriteLine("Received from server: 0", Encoding.ASCII.GetString(data));

// Close everything.
stream.Close();
client.Close();

return allBytes.ToArray();

catch (ArgumentNullException e)

Console.WriteLine("ArgumentNullException: 0", e);

catch (SocketException e)

Console.WriteLine("SocketException: 0", e);


Console.WriteLine("n Press Enter to continue...");
return new byte[0];


private static void StartTcpListener(string host, int port)

TcpListener server = null;
try

var ipHostInfo = Dns.GetHostEntry(host);
var ipAddress = ipHostInfo.AddressList[0];

// TcpListener server = new TcpListener(port);
server = new TcpListener(ipAddress, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while (true)

Console.WriteLine("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
var client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
var stream = client.GetStream();

// Buffer for reading data
var bytes = new Byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


if (allBytes.Count > 0)

Console.WriteLine("Received from client: 0", Encoding.ASCII.GetString(allBytes.ToArray()));

var received = SendReceiveRemoteServer("localhost", 11000, allBytes.ToArray());

// Send back a response.
stream.Write(received, 0, received.Length);
Console.WriteLine("Sent to client: 0", Encoding.ASCII.GetString(received));


// Shutdown and end connection
client.Close();


catch (SocketException e)

Console.WriteLine("SocketException: 0", e);

finally

// Stop listening for new clients.
server.Stop();


Console.WriteLine("nHit enter to continue...");




Although improvements should be made:



  • make it async

  • make it work with multiple TcpClients at the same time





share|improve this answer























  • Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

    – Mark Denom
    Nov 15 '18 at 11:31











  • It still gets stuck at "Found server".. Hm I wonder why..

    – Mark Denom
    Nov 15 '18 at 11:37











  • After sending sending back what I receive, it's not getting any more data for some reason

    – Mark Denom
    Nov 15 '18 at 12:07











  • In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

    – Sergiu Muresan
    Nov 15 '18 at 12:10











  • Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

    – Mark Denom
    Nov 15 '18 at 12:25











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312200%2ftcp-how-do-i-relay-the-packets-from-my-client-to-the-server%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














A simplified implementation could look like this.



public class Program

public static void Main(string args)

StartTcpListener("localhost", 9000);


private static byte SendReceiveRemoteServer(string host, int port, byte data)

try

// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
var client = new TcpClient(host, port);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

var stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent to server: 0", Encoding.ASCII.GetString(data));

// Receive the TcpServer.response.

// Read the first batch of the TcpServer response bytes.
var bytes = new byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


Console.WriteLine("Received from server: 0", Encoding.ASCII.GetString(data));

// Close everything.
stream.Close();
client.Close();

return allBytes.ToArray();

catch (ArgumentNullException e)

Console.WriteLine("ArgumentNullException: 0", e);

catch (SocketException e)

Console.WriteLine("SocketException: 0", e);


Console.WriteLine("n Press Enter to continue...");
return new byte[0];


private static void StartTcpListener(string host, int port)

TcpListener server = null;
try

var ipHostInfo = Dns.GetHostEntry(host);
var ipAddress = ipHostInfo.AddressList[0];

// TcpListener server = new TcpListener(port);
server = new TcpListener(ipAddress, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while (true)

Console.WriteLine("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
var client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
var stream = client.GetStream();

// Buffer for reading data
var bytes = new Byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


if (allBytes.Count > 0)

Console.WriteLine("Received from client: 0", Encoding.ASCII.GetString(allBytes.ToArray()));

var received = SendReceiveRemoteServer("localhost", 11000, allBytes.ToArray());

// Send back a response.
stream.Write(received, 0, received.Length);
Console.WriteLine("Sent to client: 0", Encoding.ASCII.GetString(received));


// Shutdown and end connection
client.Close();


catch (SocketException e)

Console.WriteLine("SocketException: 0", e);

finally

// Stop listening for new clients.
server.Stop();


Console.WriteLine("nHit enter to continue...");




Although improvements should be made:



  • make it async

  • make it work with multiple TcpClients at the same time





share|improve this answer























  • Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

    – Mark Denom
    Nov 15 '18 at 11:31











  • It still gets stuck at "Found server".. Hm I wonder why..

    – Mark Denom
    Nov 15 '18 at 11:37











  • After sending sending back what I receive, it's not getting any more data for some reason

    – Mark Denom
    Nov 15 '18 at 12:07











  • In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

    – Sergiu Muresan
    Nov 15 '18 at 12:10











  • Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

    – Mark Denom
    Nov 15 '18 at 12:25















1














A simplified implementation could look like this.



public class Program

public static void Main(string args)

StartTcpListener("localhost", 9000);


private static byte SendReceiveRemoteServer(string host, int port, byte data)

try

// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
var client = new TcpClient(host, port);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

var stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent to server: 0", Encoding.ASCII.GetString(data));

// Receive the TcpServer.response.

// Read the first batch of the TcpServer response bytes.
var bytes = new byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


Console.WriteLine("Received from server: 0", Encoding.ASCII.GetString(data));

// Close everything.
stream.Close();
client.Close();

return allBytes.ToArray();

catch (ArgumentNullException e)

Console.WriteLine("ArgumentNullException: 0", e);

catch (SocketException e)

Console.WriteLine("SocketException: 0", e);


Console.WriteLine("n Press Enter to continue...");
return new byte[0];


private static void StartTcpListener(string host, int port)

TcpListener server = null;
try

var ipHostInfo = Dns.GetHostEntry(host);
var ipAddress = ipHostInfo.AddressList[0];

// TcpListener server = new TcpListener(port);
server = new TcpListener(ipAddress, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while (true)

Console.WriteLine("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
var client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
var stream = client.GetStream();

// Buffer for reading data
var bytes = new Byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


if (allBytes.Count > 0)

Console.WriteLine("Received from client: 0", Encoding.ASCII.GetString(allBytes.ToArray()));

var received = SendReceiveRemoteServer("localhost", 11000, allBytes.ToArray());

// Send back a response.
stream.Write(received, 0, received.Length);
Console.WriteLine("Sent to client: 0", Encoding.ASCII.GetString(received));


// Shutdown and end connection
client.Close();


catch (SocketException e)

Console.WriteLine("SocketException: 0", e);

finally

// Stop listening for new clients.
server.Stop();


Console.WriteLine("nHit enter to continue...");




Although improvements should be made:



  • make it async

  • make it work with multiple TcpClients at the same time





share|improve this answer























  • Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

    – Mark Denom
    Nov 15 '18 at 11:31











  • It still gets stuck at "Found server".. Hm I wonder why..

    – Mark Denom
    Nov 15 '18 at 11:37











  • After sending sending back what I receive, it's not getting any more data for some reason

    – Mark Denom
    Nov 15 '18 at 12:07











  • In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

    – Sergiu Muresan
    Nov 15 '18 at 12:10











  • Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

    – Mark Denom
    Nov 15 '18 at 12:25













1












1








1







A simplified implementation could look like this.



public class Program

public static void Main(string args)

StartTcpListener("localhost", 9000);


private static byte SendReceiveRemoteServer(string host, int port, byte data)

try

// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
var client = new TcpClient(host, port);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

var stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent to server: 0", Encoding.ASCII.GetString(data));

// Receive the TcpServer.response.

// Read the first batch of the TcpServer response bytes.
var bytes = new byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


Console.WriteLine("Received from server: 0", Encoding.ASCII.GetString(data));

// Close everything.
stream.Close();
client.Close();

return allBytes.ToArray();

catch (ArgumentNullException e)

Console.WriteLine("ArgumentNullException: 0", e);

catch (SocketException e)

Console.WriteLine("SocketException: 0", e);


Console.WriteLine("n Press Enter to continue...");
return new byte[0];


private static void StartTcpListener(string host, int port)

TcpListener server = null;
try

var ipHostInfo = Dns.GetHostEntry(host);
var ipAddress = ipHostInfo.AddressList[0];

// TcpListener server = new TcpListener(port);
server = new TcpListener(ipAddress, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while (true)

Console.WriteLine("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
var client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
var stream = client.GetStream();

// Buffer for reading data
var bytes = new Byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


if (allBytes.Count > 0)

Console.WriteLine("Received from client: 0", Encoding.ASCII.GetString(allBytes.ToArray()));

var received = SendReceiveRemoteServer("localhost", 11000, allBytes.ToArray());

// Send back a response.
stream.Write(received, 0, received.Length);
Console.WriteLine("Sent to client: 0", Encoding.ASCII.GetString(received));


// Shutdown and end connection
client.Close();


catch (SocketException e)

Console.WriteLine("SocketException: 0", e);

finally

// Stop listening for new clients.
server.Stop();


Console.WriteLine("nHit enter to continue...");




Although improvements should be made:



  • make it async

  • make it work with multiple TcpClients at the same time





share|improve this answer













A simplified implementation could look like this.



public class Program

public static void Main(string args)

StartTcpListener("localhost", 9000);


private static byte SendReceiveRemoteServer(string host, int port, byte data)

try

// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
var client = new TcpClient(host, port);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

var stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent to server: 0", Encoding.ASCII.GetString(data));

// Receive the TcpServer.response.

// Read the first batch of the TcpServer response bytes.
var bytes = new byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


Console.WriteLine("Received from server: 0", Encoding.ASCII.GetString(data));

// Close everything.
stream.Close();
client.Close();

return allBytes.ToArray();

catch (ArgumentNullException e)

Console.WriteLine("ArgumentNullException: 0", e);

catch (SocketException e)

Console.WriteLine("SocketException: 0", e);


Console.WriteLine("n Press Enter to continue...");
return new byte[0];


private static void StartTcpListener(string host, int port)

TcpListener server = null;
try

var ipHostInfo = Dns.GetHostEntry(host);
var ipAddress = ipHostInfo.AddressList[0];

// TcpListener server = new TcpListener(port);
server = new TcpListener(ipAddress, port);

// Start listening for client requests.
server.Start();

// Enter the listening loop.
while (true)

Console.WriteLine("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
var client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

// Get a stream object for reading and writing
var stream = client.GetStream();

// Buffer for reading data
var bytes = new Byte[256];
var allBytes = new List<byte>();
var i = stream.Read(bytes, 0, bytes.Length);

// Loop to receive all the data sent by the client.
while (i != 0)

allBytes.AddRange(bytes);

bytes = new Byte[256];
i = stream.DataAvailable ? stream.Read(bytes, 0, bytes.Length) : 0;


if (allBytes.Count > 0)

Console.WriteLine("Received from client: 0", Encoding.ASCII.GetString(allBytes.ToArray()));

var received = SendReceiveRemoteServer("localhost", 11000, allBytes.ToArray());

// Send back a response.
stream.Write(received, 0, received.Length);
Console.WriteLine("Sent to client: 0", Encoding.ASCII.GetString(received));


// Shutdown and end connection
client.Close();


catch (SocketException e)

Console.WriteLine("SocketException: 0", e);

finally

// Stop listening for new clients.
server.Stop();


Console.WriteLine("nHit enter to continue...");




Although improvements should be made:



  • make it async

  • make it work with multiple TcpClients at the same time






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:20









Sergiu MuresanSergiu Muresan

3596




3596












  • Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

    – Mark Denom
    Nov 15 '18 at 11:31











  • It still gets stuck at "Found server".. Hm I wonder why..

    – Mark Denom
    Nov 15 '18 at 11:37











  • After sending sending back what I receive, it's not getting any more data for some reason

    – Mark Denom
    Nov 15 '18 at 12:07











  • In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

    – Sergiu Muresan
    Nov 15 '18 at 12:10











  • Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

    – Mark Denom
    Nov 15 '18 at 12:25

















  • Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

    – Mark Denom
    Nov 15 '18 at 11:31











  • It still gets stuck at "Found server".. Hm I wonder why..

    – Mark Denom
    Nov 15 '18 at 11:37











  • After sending sending back what I receive, it's not getting any more data for some reason

    – Mark Denom
    Nov 15 '18 at 12:07











  • In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

    – Sergiu Muresan
    Nov 15 '18 at 12:10











  • Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

    – Mark Denom
    Nov 15 '18 at 12:25
















Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

– Mark Denom
Nov 15 '18 at 11:31





Oh, yeah I was doing pretty much the same thing I just couldnt figure out how to send the data back, I'll read through your example a bit more.

– Mark Denom
Nov 15 '18 at 11:31













It still gets stuck at "Found server".. Hm I wonder why..

– Mark Denom
Nov 15 '18 at 11:37





It still gets stuck at "Found server".. Hm I wonder why..

– Mark Denom
Nov 15 '18 at 11:37













After sending sending back what I receive, it's not getting any more data for some reason

– Mark Denom
Nov 15 '18 at 12:07





After sending sending back what I receive, it's not getting any more data for some reason

– Mark Denom
Nov 15 '18 at 12:07













In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

– Sergiu Muresan
Nov 15 '18 at 12:10





In this example the connection is closed after the message is send, you need to keep the connection open and check if there is any new data coming through.

– Sergiu Muresan
Nov 15 '18 at 12:10













Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

– Mark Denom
Nov 15 '18 at 12:25





Yeah I know, I changed that and it gets to the point where it should read but it just stops there.

– Mark Denom
Nov 15 '18 at 12:25



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312200%2ftcp-how-do-i-relay-the-packets-from-my-client-to-the-server%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20