Last active
April 20, 2019 10:47
-
-
Save b1oki/672cd42da47ed649e94371ab109de812 to your computer and use it in GitHub Desktop.
C# TCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
namespace TestClient | |
{ | |
internal static class Program | |
{ | |
const int PORT_NO = 5000; | |
const string SERVER_IP = "127.0.0.1"; | |
private static void Main(string[] args) | |
{ | |
Console.WriteLine("---data to send to the server---"); | |
string textToSend = DateTime.Now.ToString(); | |
Console.WriteLine("--- create a TCPClient object at the IP and port no.---"); | |
TcpClient client = new TcpClient(SERVER_IP, PORT_NO); | |
NetworkStream nwStream = client.GetStream(); | |
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend); | |
Console.WriteLine("--- send the text ---"); | |
Console.WriteLine("Sending : " + textToSend); | |
nwStream.Write(bytesToSend, 0, bytesToSend.Length); | |
Console.WriteLine("--- read back the text ---"); | |
byte[] bytesToRead = new byte[client.ReceiveBufferSize]; | |
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize); | |
client.Close(); | |
Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead)); | |
Console.WriteLine("--- this is the end ---"); | |
//Console.ReadLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
namespace TestServer | |
{ | |
class Program | |
{ | |
const int PORT_NO = 5000; | |
const string SERVER_IP = "127.0.0.1"; | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("--- listen at the specified IP and port no. ---"); | |
IPAddress localAdd = IPAddress.Parse(SERVER_IP); | |
TcpListener listener = new TcpListener(localAdd, PORT_NO); | |
Console.WriteLine("Listening..."); | |
listener.Start(); | |
Console.WriteLine("--- incoming client connected ---"); | |
TcpClient client = listener.AcceptTcpClient(); | |
Console.WriteLine("--- get the incoming data through a network stream ---"); | |
NetworkStream nwStream = client.GetStream(); | |
byte[] buffer = new byte[client.ReceiveBufferSize]; | |
Console.WriteLine("--- read incoming stream ---"); | |
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); | |
Console.WriteLine("--- convert the data received into a string ---"); | |
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); | |
Console.WriteLine("Received : " + dataReceived); | |
Console.WriteLine("--- write back the text to the client ---"); | |
Console.WriteLine("Sending back : " + dataReceived); | |
nwStream.Write(buffer, 0, bytesRead); | |
client.Close(); | |
listener.Stop(); | |
Console.WriteLine("--- this is the end ---"); | |
//Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment