Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
ChatServer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SE
ChatProgram
ChatServer
Commits
8aa9b3f9
Commit
8aa9b3f9
authored
4 years ago
by
Alexander Ibach
Browse files
Options
Downloads
Patches
Plain Diff
Implemented basic chat server
parent
f712924a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
ChatServer.sln
+25
-0
25 additions, 0 deletions
ChatServer.sln
ChatServer/ChatServer.csproj
+8
-0
8 additions, 0 deletions
ChatServer/ChatServer.csproj
ChatServer/Server.cs
+68
-0
68 additions, 0 deletions
ChatServer/Server.cs
with
101 additions
and
0 deletions
ChatServer.sln
0 → 100644
+
25
−
0
View file @
8aa9b3f9
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
This diff is collapsed.
Click to expand it.
ChatServer/ChatServer.csproj
0 → 100644
+
8
−
0
View file @
8aa9b3f9
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
</Project>
This diff is collapsed.
Click to expand it.
ChatServer/Server.cs
0 → 100644
+
68
−
0
View file @
8aa9b3f9
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment