TCP Socket Tutorial (C#, Visual Studio, Networked Console Apps)
Source from wolfs cry games
client app code
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace ClientSocketApp
{
class Program
{
static void Main(string[] args)
{
connection:
try
{
TcpClient client = new TcpClient("127.0.0.1", 1302);
string messageToSend = "My name is Neo";
int byteCount = Encoding.ASCII.GetByteCount(messageToSend + 1);
byte[] sendData = Encoding.ASCII.GetBytes(messageToSend);
NetworkStream stream = client.GetStream();
stream.Write(sendData, 0, sendData.Length);
Console.WriteLine("sending data to server...");
StreamReader sr = new StreamReader(stream);
string response = sr.ReadLine();
Console.WriteLine(response);
stream.Close();
client.Close();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("failed to connect...");
goto connection;
}
}
}
}
server app code
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
namespace ServerSocketApp
{
class Program
{
static void Main(string[] args)
{
TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 1302);
listener.Start();
while (true)
{
Console.WriteLine("Waiting for a connection.");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Client accepted.");
NetworkStream stream = client.GetStream();
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
try
{
byte[] buffer = new byte[1024];
stream.Read(buffer, 0, buffer.Length);
int recv = 0;
foreach (byte b in buffer)
{
if (b!=0)
{
recv++;
}
}
string request = Encoding.UTF8.GetString(buffer, 0, recv);
Console.WriteLine("request received");
sw.WriteLine("You rock!");
sw.Flush();
}
catch(Exception e)
{
Console.WriteLine("Something went wrong.");
sw.WriteLine(e.ToString());
}
}
}
}
}
Ulasan