oo-70-245-1

oo-70-245-1 Commit Details


Date:2015-11-23 19:19:04 (9 years 4 months ago)
Author:Natalie Adams
Branch:master
Commit:52053902edd3eb047202782f1e9da8765f6a8163
Parents: 7268bfc02009f62f77f8b3d30d546aaf3a2822d4
Message:Adding tcp example

Changes:

File differences

TCP/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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace tcpclientexample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
                Console.WriteLine("Connecting....");
                tcpClient.Connect("127.0.0.1", 8888);
                Console.WriteLine("Connected");
                Console.Write("Enter the string to be transmitted: ");
                String str = Console.ReadLine();
                Stream stm = tcpClient.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                Console.WriteLine("Transmitting....");
                stm.Write(ba, 0, ba.Length);
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                for (int i = 0; i < k; i++)
                {
                    Console.Write(Convert.ToChar(bb[i]));
                }
                tcpClient.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.StackTrace);
            }
        }
    }
}
TCP/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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace tcpserverexample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8888);
                tcpListener.Start();
                Console.WriteLine("The server is running at port 8888...");
                Console.WriteLine("The local End point is :" + tcpListener.LocalEndpoint);
                Console.WriteLine("Waiting for a connection...");
                Socket client = tcpListener.AcceptSocket();
                Console.WriteLine("Connection accepted from " + client.RemoteEndPoint);
                byte[] b = new byte[100];
                int k = client.Receive(b);
                Console.WriteLine("Received...");
                for (int i = 0; i < k; i++)
                {
                    Console.Write(Convert.ToChar(b[i]));
                }
                ASCIIEncoding asen = new ASCIIEncoding();
                client.Send(asen.GetBytes("The string was recieved by the server"));
                Console.WriteLine("Sent Acknolwedgement");
                client.Close();
                tcpListener.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.StackTrace);
            }
        }
    }
}

Archive Download the corresponding diff file

Branches

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