Skip to content

Commit 2a357dd

Browse files
committed
Send handshake successfully to a given server
1 parent 7cde3ca commit 2a357dd

File tree

7 files changed

+148
-13
lines changed

7 files changed

+148
-13
lines changed

MCQuery-Lib/Connection.cs

+121-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,135 @@
1-
using System.Net.Sockets;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Sockets;
6+
using System.Text;
27

3-
namespace MCQuery_Lib
8+
namespace MCQuery
49
{
510
public class Connection
611
{
7-
public Connection(string address, string port)
8-
{
9-
TcpClient client = new TcpClient(address, int.Parse(port));
12+
private byte[] Magic = { 0xFE, 0xFD };
13+
private byte Handshake = 0x09;
14+
private byte Stat = 0x00;
15+
private byte[] SessionID = { 0x01, 0x01, 0x01, 0x01 };
1016

17+
public Connection(string address, int port)
18+
{
1119
try
1220
{
13-
// client.Connect();
21+
string udpResponse = sendByUdp(address, port);
22+
23+
if (udpResponse.Equals(String.Empty))
24+
{
25+
string tcpResponse = sendByTcp(address, port);
26+
27+
if (tcpResponse.Equals(String.Empty)) throw new NotImplementedException();
28+
}
29+
else
30+
{
31+
// Uses the IPEndPoint object to determine which of these two hosts responded.
32+
Console.WriteLine("This is the message you received " +
33+
udpResponse.ToString());
34+
Console.WriteLine("This message was sent from " +
35+
address +
36+
" on their port number " +
37+
port.ToString());
38+
}
1439
}
1540
catch (SocketException socketException)
1641
{
17-
//TODO:Do something with exception...
42+
throw new SocketException();
43+
}
44+
}
45+
46+
public Server getServer()
47+
{
48+
//TODO: Return server object with fetched data from the request.
49+
return new Server("dummy", true);
50+
}
51+
52+
public string sendByUdp(string address, int port)
53+
{
54+
try
55+
{
56+
//Check if address is a domain name.
57+
if (isDomainAddress(address))
58+
{
59+
address = getIpFromDomain(address);
60+
}
61+
62+
UdpClient udpClient = new UdpClient();
63+
64+
udpClient.Connect(address, port);
65+
66+
List<byte> message = new List<byte>();
67+
message.AddRange(Magic);
68+
message.Add(Handshake);
69+
message.Add(Stat);
70+
message.AddRange(SessionID);
71+
72+
Byte[] handshakeMessage = message.ToArray().ToArray();
73+
74+
udpClient.Send(handshakeMessage, handshakeMessage.Length);
75+
76+
//IPEndPoint object will allow us to read datagrams sent from any source.
77+
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(address), port);
78+
79+
// Blocks until a message returns on this socket from a remote host.
80+
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
81+
string returnData = Encoding.ASCII.GetString(receiveBytes);
82+
83+
udpClient.Close();
84+
85+
if (returnData.Equals(String.Empty))
86+
{
87+
return String.Empty;
88+
}
89+
else
90+
{
91+
return returnData;
92+
}
93+
}
94+
catch (SocketException exception)
95+
{
96+
throw new SocketException();
97+
}
98+
}
99+
100+
private string getIpFromDomain(string address)
101+
{
102+
IPAddress[] addresses = Dns.GetHostAddresses(address);
103+
104+
return addresses[0].AddressFamily.ToString();
105+
}
106+
107+
private bool isDomainAddress(string address)
108+
{
109+
try
110+
{
111+
IPHostEntry hostEntry = Dns.GetHostEntry(address);
112+
113+
if (hostEntry.HostName == address)
114+
{
115+
return true;
116+
}
117+
else
118+
{
119+
return false;
120+
}
121+
}
122+
catch (Exception exception)
123+
{
124+
Console.WriteLine(exception.Message);
125+
throw new SocketException();
18126
}
19127
}
128+
129+
public string sendByTcp(string address, int port)
130+
{
131+
//TODO: Implement sending packet by TCP.
132+
return String.Empty;
133+
}
20134
}
21135
}
File renamed without changes.

MCQuery-Lib/Request.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66

7-
namespace MCQuery_Lib
7+
namespace MCQuery
88
{
99
public class Request
1010
{

MCQuery-Lib/Server.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66

7-
namespace MCQuery_Lib
7+
namespace MCQuery
88
{
99
public class Server
1010
{

MCQuery.sln

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.27130.2024
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCQuery-Console", "MCQuery\MCQuery-Console.csproj", "{D2B7C12E-E5CC-4517-B07A-AE9AE9A0DDAD}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MCQuery-Console", "MCQuery\MCQuery-Console.csproj", "{D2B7C12E-E5CC-4517-B07A-AE9AE9A0DDAD}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCQuery-Lib", "MCQuery-Lib\MCQuery-Lib.csproj", "{B439EF43-29DE-4490-9542-83111E9CF88B}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCQuery", "MCQuery-Lib\MCQuery.csproj", "{B439EF43-29DE-4490-9542-83111E9CF88B}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution

MCQuery/MCQuery-Console.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<ProjectReference Include="..\MCQuery-Lib\MCQuery-Lib.csproj" />
9+
<ProjectReference Include="..\MCQuery-Lib\MCQuery.csproj" />
1010
</ItemGroup>
1111

1212
</Project>

MCQuery/Program.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using MCQuery;
2+
using System;
23

34
namespace MCQuery
45
{
@@ -7,6 +8,26 @@ class Program
78
static void Main(string[] args)
89
{
910
Console.WriteLine("Hello World!");
11+
12+
Console.WriteLine("Server IP Address: ");
13+
string ipAddress = Console.ReadLine();
14+
15+
Console.WriteLine("Server Port: ");
16+
string portString = Console.ReadLine();
17+
18+
int.TryParse(portString, out int port);
19+
20+
if (port != 0)
21+
{
22+
Connection connection = new Connection(ipAddress, port); //This should give us a challenge token needed for getting data from the sever.
23+
}
24+
else
25+
{
26+
Console.WriteLine("Wrong port number!");
27+
}
28+
29+
Console.WriteLine("\n Press any key to continue...");
30+
Console.Read();
1031
}
1132
}
1233
}

0 commit comments

Comments
 (0)