Connection Issues

PureSc0pe

Well-known member
Joined
Mar 17, 2004
Messages
161
Location
Arizona
I have designed a server side program for a computer game. I need it to connect to the game to send commands. I have a few tcpclient scripts, but I need to know how to connect to the game through the ip and port of it. In this case the ip would be 127.0.0.1 and the port would be the default game port of 28960. When you connect to 127.0.0.1:28960 you then need to send the password to login to the game...How do I communicate with the game to do so? If you have any examples or can help please post on what I need to do...

Attached is a tcpclient thing I have found, but I need it to connect with the game and send the given password to login to the console to then execute in game commands..
 
Last edited by a moderator:
here you go

Read the following article on using sockets in non-blocking mode in .NET.

http://www.codeproject.com/csharp/socketsincs.asp

Be sure to follow good habits with your tcp coding (i.e. message terminators or fixed lengths, sending and receiving entire messages, connection shutdowns).

A simple way to structure your messages would just be to send strings through the sockets and end them with a terminator (like \r\n or another char thats not going to appear in your messages). Then when youre doing your receives just keep receiving until you read in the terminator (concating if you only get a partial on a recv).

i.e. pseudocode (use stringbuilder not string if youve got big strings going back and forth):

string DataRecv = "";
byte [] rcvBuf = new byte[256];
while (true)
{
int nRead = sock.receive(rcvBuf, 0, 256);
DataRecv += System.Text.Encoding.ASCII.GetString(rcvBuf, 0, nRead);
if (DataRecv.EndWith("\r\n"))
break;
}

//ok now youve got the whole message


and be sure you send an entire message in your send something like this:

string toSend = "Data to send \r\n";
byte [] sndBuf = System.Text.Encoding.ASCII.GetBytes(toSend);
int nSent = 0;
while (nSent < sndBuf.Length)
{
nSent += sock.Send(sndBuf, nSent, sndBuf.Length - nSent);
}



Tim
 
Back
Top