I'm trying to connect to a server using a IP and Port. I would like to create a TcpClient
connection to send and receive packets later on.
I created this in PHP before using the fsocket
functions. For example:
$fp = fsockopen(packets::$server, packets::$port, $errno, $errstr, 30);
fwrite($fp, packets::login('username', 'password'));
Well now I want to redo this using C#. I started to google about this but literally only found about a way using the TcpClient class
So I started like the following:
public void tcpConnect(string ip, int port)
{
TcpClient connection = new TcpClient();
connection.Connect(ip, port);
}
I'm callingthis function in the following format:
functions tcp = new functions();
tcp.tcpConnect("74.113.233.195", 9061);
How would i continue to send packets to this tcp connection?
In PHP i used to create the packets in the following format:
public static function login($username, $password) {
$packet = "<msg t='sys'>";
$packet .= "<body action='login' r='0'>";
$packet .= "<login z='simpleChat'>";
$packet .= "<nick>";
$packet .= "<![CDATA[" . $username . "]]>";
$packet .= "</nick>";
$packet .= "<pword>";
$packet .= "<![CDATA[" . $password . "]]>";
$packet .= "</pword>";
$packet .= "</login>";
$packet .= "</body>";
$packet .= "</msg>";
$packet .= chr(0);
return $packet;
}
Which I used to add to the socket in this format:
fwrite($fp, packets::login('username', 'password'));
How would i get this working using c#?
Update:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace bot_console
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Bot started...");
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("74.113.233.195", 9061);
byte[] login = Encoding.ASCII.GetBytes("<msg t='sys'><body action='login' r='0'><login z='simpleChat'><nick><![CDATA[turtle-1]]></nick><pword><![CDATA[password]]></pword></login></body></msg>");
byte[] navigate = Encoding.ASCII.GetBytes("{\"b\":{\"x\":\"cluster\",\"p\":{\"roomName\":\"|2.1.world1017.school1@|30|2|A\",\"password\":\"\"},\"c\":\"enterRoom\",\"r\":\"-1\"},\"t\":\"xt\"}");
socket.Send(login);
socket.Send(navigate);
Console.ReadLine();
}
}
}
That's the actual code, sadly it doesnt seem to send the packets. Anyone an idea?
You can refer this: Socket Send and Receive
Socket.Send method
public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
int startTickCount = Environment.TickCount;
int sent = 0; // how many bytes is already sent
do {
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try {
sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably full, wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (sent < size);
}
and
Socket.Receive method
public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
int startTickCount = Environment.TickCount;
int received = 0; // how many bytes is already received
do {
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try {
received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably empty, wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (received < size);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.