Skip to content
Snippets Groups Projects
Commit 8aa9b3f9 authored by Alexander Ibach's avatar Alexander Ibach
Browse files

Implemented basic chat server

parent f712924a
No related branches found
No related tags found
No related merge requests found

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29409.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatServer", "ChatServer\ChatServer.csproj", "{8ADEEE9C-D236-4AB9-AADA-A0BC3BBDE334}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8ADEEE9C-D236-4AB9-AADA-A0BC3BBDE334}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8ADEEE9C-D236-4AB9-AADA-A0BC3BBDE334}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8ADEEE9C-D236-4AB9-AADA-A0BC3BBDE334}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8ADEEE9C-D236-4AB9-AADA-A0BC3BBDE334}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {73464EFF-BC95-4127-9D23-3945E404A254}
EndGlobalSection
EndGlobal
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace ChatServer
{
public class Server
{
static void Main(string[] args)
{
// Set the TcpListener to port and local adress.
Int32 port = 1200;
System.Net.IPAddress localAddress = System.Net.IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(localAddress, port);
int requestCount = 0;
TcpClient client = default;
// start server
server.Start();
Console.WriteLine(" $ Server running...");
client = server.AcceptTcpClient();
Console.WriteLine(" $ Client connected...");
requestCount = 0;
// waiting on request
while ((true))
{
try
{
// counting requests in private member
requestCount += 1;
// get stream and read, retrieve message content without $
NetworkStream networkStream = client.GetStream();
byte[] bytesFrom = new byte[65536];
networkStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
// print to server console
Console.WriteLine(" $ Received a message from client in request " + requestCount.ToString() + " with content: " + dataFromClient);
// send response
string response = "Message " + requestCount.ToString() + " from client with content: " + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(response);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
// print to server console
Console.WriteLine(" $ " + response);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
client.Close();
server.Stop();
Console.WriteLine(" $ exit");
Console.ReadLine();
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment