- Joined
- Jun 21, 2010
- Messages
- 324
- Reaction score
- 54
Hello!
I'm having a bit of trouble working with sockets and TCP connections with C#. The idea is this:
1. Server is listening for clients
2. Client requests connection, connection is accepted.
3. Client sends first packet (let's call that the hello packet)
4. Server sends a reply back to acknowledge the client
5. Server waits for a reply from client and writes the reply to the console.
I'm quite new to sockets in C#, however I really want to get into it. So far, I have everything working fine, apart from the fact, that I don't know how to send a second packet. If I repeat steps 3 and 4, the code eventually throws an error about not being able to read data from the transport connection.
I have a slight feeling that something needs to be cleared or something similar, but I don't know what it is.
If it helps, this server is accepting connections from a Habbo client.
This is code I have so far:
I'm having a bit of trouble working with sockets and TCP connections with C#. The idea is this:
1. Server is listening for clients
2. Client requests connection, connection is accepted.
3. Client sends first packet (let's call that the hello packet)
4. Server sends a reply back to acknowledge the client
5. Server waits for a reply from client and writes the reply to the console.
I'm quite new to sockets in C#, however I really want to get into it. So far, I have everything working fine, apart from the fact, that I don't know how to send a second packet. If I repeat steps 3 and 4, the code eventually throws an error about not being able to read data from the transport connection.
I have a slight feeling that something needs to be cleared or something similar, but I don't know what it is.
If it helps, this server is accepting connections from a Habbo client.
This is code I have so far:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Gingerbread
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Hello! Gingerbread";
TcpListener server = null;
try
{
IPAddress address = IPAddress.Parse("127.0.0.1");
int port = 1337;
server = new TcpListener(address, port);
server.Start();
byte[] bytes = new byte[256];
string data = string.Empty;
while (true)
{
Console.WriteLine("Server successfully listening on {0}:{1}", address.ToString(), port);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connection from: {0}", client.Client.RemoteEndPoint.ToString());
data = null;
NetworkStream stream = client.GetStream();
// packet 1
// get xml policy request
int bytes_p1 = stream.Read(bytes, 0, bytes.Length);
data = Encoding.ASCII.GetString(bytes, 0, bytes_p1);
Console.WriteLine(data);
// reply 1
// send xml policy request
string reply = "<?xml version=\"1.0\"?>\r\n" + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\r\n" + "<cross-domain-policy>\r\n" + "<allow-access-from domain=\"*\" to-ports=\"1-31111\" />\r\n" + "</cross-domain-policy>\x0";
byte[] msg = Encoding.ASCII.GetBytes(reply);
stream.Write(msg, 0, msg.Length);
stream.Flush();
// packet 2
// get unknown message
int bytes_p2 = stream.Read(bytes, 0, bytes.Length);
data = Encoding.ASCII.GetString(bytes, 0, bytes_p2);
Console.WriteLine(data);
}
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
finally
{
server.Stop();
}
Console.Read();
}
}
}