oo-70-245-1

oo-70-245-1 Commit Details


Date:2018-10-17 13:56:55 (5 years 11 months ago)
Author:Natalie Adams
Branch:master
Commit:9133efac475f71a7cd7960929cbfbd9d4f914ee3
Parents: e64f5855e7b5ad229f68cdfe28b5ccabcf59925f
Message:adding tcp examples

Changes:

File differences

TCP/Example1/client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PongExample1Client
{
public static class ByteExtension
{
public static string GetString(this byte[] arr, int k)
{
return Encoding.ASCII.GetString(arr, 0, k);
}
}
public static class StringExtension
{
public static byte[] ToByteArray(this string str)
{
return Encoding.ASCII.GetBytes(str);
}
}
class Client
{
TcpClient client;
int count = 0;
public Client(IPAddress ip, int port = 5000)
{
client = new TcpClient();
client.Connect(ip, port);
}
public Client(int port = 5000) : this(IPAddress.Loopback, port) { }
public Client() : this(IPAddress.Loopback, 5000) { }
public bool Respond()
{
byte[] buffer = new byte[1024];
byte[] pong = "ping".ToByteArray();
NetworkStream clientStream = client.GetStream();
clientStream.Write(pong, 0, pong.Length);
Console.WriteLine("Sent {0} to the server - {1}", pong.GetString(pong.Length), count);
Thread.Sleep(2000);
int k = clientStream.Read(buffer, 0, 1024);
Console.WriteLine("Received {0} from the server - {1}", buffer.GetString(k), count);
count++;
return true;
}
}
class Program
{
static void Main(string[] args)
{
Client c = new Client();
while (c.Respond()) ;
}
}
}
TCP/Example1/server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PongExample1
{
public static class ByteExtension
{
public static string GetString(this byte[] arr, int k)
{
return Encoding.ASCII.GetString(arr, 0, k);
}
}
public static class StringExtension
{
public static byte[] ToByteArray(this string str)
{
return Encoding.ASCII.GetBytes(str);
}
}
class Server
{
TcpListener server;
Socket s;
int count = 0;
public Server(IPAddress ip, int port = 5000)
{
server = new TcpListener(ip, port);
server.Start();
Console.WriteLine("Listening on {0} on port {1}", ip.ToString(), port);
s = server.AcceptSocket();
}
public Server(int port = 5000) : this(IPAddress.Loopback, port) { }
public Server () : this(IPAddress.Loopback, 5000) { }
public bool Respond()
{
byte[] buffer = new byte[1024];
byte[] pong = "pong".ToByteArray();
int k = s.Receive(buffer);
Console.WriteLine("Client sent - {0} - {1}", buffer.GetString(k), count);
Thread.Sleep(2000);
s.Send(pong);
Console.WriteLine("Sent to client - {0} - {1}", pong.GetString(pong.Length), count);
count++;
return true;
}
}
class Program
{
static void Main(string[] args)
{
Server s = new Server();
while (s.Respond()) ;
}
}
}
TCP/Example2/client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PongExample1Client
{
public static class ByteExtension
{
public static string GetString(this byte[] arr, int k)
{
return Encoding.ASCII.GetString(arr, 0, k);
}
}
public static class StringExtension
{
public static byte[] ToByteArray(this string str)
{
return Encoding.ASCII.GetBytes(str);
}
}
class Client
{
TcpClient client;
int count = 0;
public Client(IPAddress ip, int port = 5000)
{
client = new TcpClient();
client.Connect(ip, port);
}
public Client(int port = 5000) : this(IPAddress.Loopback, port) { }
public Client() : this(IPAddress.Loopback, 5000) { }
public bool Respond()
{
byte[] buffer = new byte[1024];
byte[] pong = "ping".ToByteArray();
NetworkStream clientStream = client.GetStream();
clientStream.Write(pong, 0, pong.Length);
Console.WriteLine("Sent {0} to the server - {1}", pong.GetString(pong.Length), count);
Thread.Sleep(2000);
int k = clientStream.Read(buffer, 0, 1024);
Console.WriteLine("Received {0} from the server - {1}", buffer.GetString(k), count);
count++;
return true;
}
}
class Program
{
static void Main(string[] args)
{
Client c = new Client();
while (c.Respond()) ;
}
}
}
TCP/Example2/server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PongExample1
{
public static class ByteExtension
{
public static string GetString(this byte[] arr, int k)
{
return Encoding.ASCII.GetString(arr, 0, k);
}
}
public static class StringExtension
{
public static byte[] ToByteArray(this string str)
{
return Encoding.ASCII.GetBytes(str);
}
}
class Server
{
TcpListener server;
Socket s;
int count = 0;
public Server(IPAddress ip, int port = 5000)
{
server = new TcpListener(ip, port);
server.Start();
Console.WriteLine("Listening on {0} on port {1}", ip.ToString(), port);
while (true)
{
s = server.AcceptSocket();
Thread t = new Thread(Respond);
t.Start(s);
}
}
public Server(int port = 5000) : this(IPAddress.Loopback, port) { }
public Server() : this(IPAddress.Loopback, 5000) { }
public void Respond(object socket)
{
Socket s = (Socket)socket;
Console.WriteLine("Accepted connection from {0}", s.RemoteEndPoint);
while (true)
{
byte[] buffer = new byte[1024];
byte[] pong = "pong".ToByteArray();
int k = s.Receive(buffer);
Console.WriteLine("Client sent - {0} - {1}", buffer.GetString(k), count);
Thread.Sleep(2000);
s.Send(pong);
Console.WriteLine("Sent to client - {0} - {1}", pong.GetString(pong.Length), count);
count++;
}
}
}
class Program
{
static void Main(string[] args)
{
Server s = new Server();
}
}
}
TCP/Example3/client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ServerExample11
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient client = new TcpClient();
client.Connect(IPAddress.Loopback, 5000);
Console.WriteLine("Connected to server {0} on port 5000", IPAddress.Loopback);
Console.WriteLine("Sending data...");
Console.WriteLine("Enter the string to send:");
string input = Console.ReadLine();
byte[] msg = Encoding.ASCII.GetBytes(input);
byte[] buffer = new byte[1024];
NetworkStream stream = client.GetStream();
stream.Write(msg, 0, msg.Length);
int k = stream.Read(buffer, 0, 1024);
Console.WriteLine("Received from server: {0}", Encoding.ASCII.GetString(buffer, 0, k));
} catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
TCP/Example3/server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ClientExample11
{
class Program
{
static void Main(string[] args)
{
try
{
TcpListener server = new TcpListener(IPAddress.Loopback, 5000);
server.Start();
Console.WriteLine("Server is listening on port 5000...");
Console.WriteLine("The local End point is: {0}", server.LocalEndpoint);
Console.WriteLine("Waiting for a connection....");
while (true)
{
Thread t = new Thread(handleConnection);
Socket client = server.AcceptSocket();
t.Start(client);
}
server.Stop();
} catch (Exception e)
{
Console.WriteLine(e);
}
}
static void handleConnection(object socket)
{
Socket client = (Socket)socket;
Console.WriteLine("Connection accepted from {0}", client.RemoteEndPoint);
byte[] buffer = new byte[1024];
int k = client.Receive(buffer);
Console.WriteLine("Received....");
Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, k));
client.Send(Encoding.ASCII.GetBytes("The string was received by the server."));
Console.WriteLine("\nSent Acknowledgement");
client.Close();
}
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.09078s using 14 queries.