Skip to content

Commit 16d0013

Browse files
AmbratolmAmbratolm
authored andcommitted
First Commit
0 parents  commit 16d0013

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+965
-0
lines changed

Client/Client API/Client.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+

2+
namespace Client
3+
{
4+
public static class Client
5+
{
6+
private static TcpClientLite _client;
7+
8+
public static void CreateTcpClient(string ip, int port)
9+
{
10+
_client = new TcpClientLite(ip, port);
11+
ClientLog.ClientCreated(ip, port);
12+
}
13+
14+
public static void Connect(string ip, int port)
15+
{
16+
_client.Connect(ip, port);
17+
ClientLog.ClientConnected(_client.LocalIP, _client.LocalPort,
18+
_client.RemoteIP, _client.RemotePort);
19+
}
20+
21+
public static void SendText(string text)
22+
{
23+
int size = _client.SendText(text);
24+
ClientLog.BytesSent(size);
25+
}
26+
27+
public static void Close()
28+
{
29+
_client.Close();
30+
ClientLog.ClientClosed();
31+
}
32+
}
33+
}

Client/Client API/ClientLog.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using SharedAPI;
2+
3+
namespace Client
4+
{
5+
public static class ClientLog
6+
{
7+
public static void New()
8+
{
9+
Log.New("Client");
10+
Log.Header("♦ Client");
11+
}
12+
13+
public static void ClientCreated(string ip, int port)
14+
{
15+
Log.Note("New client created as Client@{0}:{1}.", ip, port);
16+
Log.Warning("Connecting...");
17+
}
18+
19+
public static void ClientConnected(string localIP, int localPort,
20+
string remoteIP, int remotePort)
21+
{
22+
Log.SetCursorToPreviousLine();
23+
Log.ClearLine();
24+
Log.Success("Client@{0}:{1} connected to Server@{2}:{3}.",
25+
localIP, localPort, remoteIP, remotePort);
26+
}
27+
28+
public static void BytesSent(int size)
29+
{
30+
Log.Info("{0} bytes sent.", size);
31+
}
32+
33+
public static void ClientClosed()
34+
{
35+
Log.Warning("Client closed.");
36+
}
37+
}
38+
}

Client/Client API/TcpClientLite.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Net;
2+
using System.Net.Sockets;
3+
using System.Text;
4+
5+
namespace Client
6+
{
7+
public class TcpClientLite
8+
{
9+
private TcpClient _tcpClient;
10+
11+
public string LocalIP { get; private set; }
12+
public int LocalPort { get; private set; }
13+
public string RemoteIP { get; private set; }
14+
public int RemotePort { get; private set; }
15+
16+
public TcpClientLite(string localIP = "127.0.0.1 ", int localPort = 80)
17+
{
18+
this.LocalIP = localIP;
19+
this.LocalPort = localPort;
20+
_tcpClient = new TcpClient(new IPEndPoint(IPAddress.Parse(localIP), localPort));
21+
}
22+
23+
public void Connect(string remoteIP = "127.0.0.2 ", int remotePort = 81)
24+
{
25+
this.RemoteIP = remoteIP;
26+
this.RemotePort = remotePort;
27+
_tcpClient.Connect(remoteIP, remotePort);
28+
}
29+
30+
public int SendText(string text)
31+
{
32+
byte[] buffer = Encoding.UTF8.GetBytes(text);
33+
NetworkStream stream = _tcpClient.GetStream();
34+
stream.Write(buffer, 0, buffer.Length);
35+
return buffer.Length;
36+
}
37+
38+
public void Close()
39+
{
40+
_tcpClient.Close();
41+
}
42+
}
43+
}

Client/Client.csproj

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{7293DD8E-888C-46BE-A4F6-B94FD0817092}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Client</RootNamespace>
12+
<AssemblyName>Client</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
<PublishUrl>publish\</PublishUrl>
17+
<Install>true</Install>
18+
<InstallFrom>Disk</InstallFrom>
19+
<UpdateEnabled>false</UpdateEnabled>
20+
<UpdateMode>Foreground</UpdateMode>
21+
<UpdateInterval>7</UpdateInterval>
22+
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
23+
<UpdatePeriodically>false</UpdatePeriodically>
24+
<UpdateRequired>false</UpdateRequired>
25+
<MapFileExtensions>true</MapFileExtensions>
26+
<ApplicationRevision>0</ApplicationRevision>
27+
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
28+
<IsWebBootstrapper>false</IsWebBootstrapper>
29+
<UseApplicationTrust>false</UseApplicationTrust>
30+
<BootstrapperEnabled>true</BootstrapperEnabled>
31+
</PropertyGroup>
32+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
33+
<PlatformTarget>x86</PlatformTarget>
34+
<DebugSymbols>true</DebugSymbols>
35+
<DebugType>full</DebugType>
36+
<Optimize>false</Optimize>
37+
<OutputPath>bin\Debug\</OutputPath>
38+
<DefineConstants>DEBUG;TRACE</DefineConstants>
39+
<ErrorReport>prompt</ErrorReport>
40+
<WarningLevel>4</WarningLevel>
41+
</PropertyGroup>
42+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
43+
<PlatformTarget>x86</PlatformTarget>
44+
<DebugType>pdbonly</DebugType>
45+
<Optimize>true</Optimize>
46+
<OutputPath>bin\Release\</OutputPath>
47+
<DefineConstants>TRACE</DefineConstants>
48+
<ErrorReport>prompt</ErrorReport>
49+
<WarningLevel>4</WarningLevel>
50+
</PropertyGroup>
51+
<PropertyGroup>
52+
<ApplicationIcon>icon.ico</ApplicationIcon>
53+
</PropertyGroup>
54+
<ItemGroup>
55+
<Reference Include="System" />
56+
<Reference Include="System.Core" />
57+
<Reference Include="System.Xml.Linq" />
58+
<Reference Include="System.Data.DataSetExtensions" />
59+
<Reference Include="Microsoft.CSharp" />
60+
<Reference Include="System.Data" />
61+
<Reference Include="System.Xml" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<Compile Include="Client API\Client.cs" />
65+
<Compile Include="Client API\ClientLog.cs" />
66+
<Compile Include="Client API\TcpClientLite.cs" />
67+
<Compile Include="Program.cs" />
68+
<Compile Include="Properties\AssemblyInfo.cs" />
69+
</ItemGroup>
70+
<ItemGroup>
71+
<Content Include="icon.ico" />
72+
</ItemGroup>
73+
<ItemGroup>
74+
<ProjectReference Include="..\SharedAPI\SharedAPI.csproj">
75+
<Project>{2BC66B28-59F2-4A14-AACC-F2D545346D8B}</Project>
76+
<Name>SharedAPI</Name>
77+
</ProjectReference>
78+
</ItemGroup>
79+
<ItemGroup>
80+
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">
81+
<Visible>False</Visible>
82+
<ProductName>Microsoft .NET Framework 4 Client Profile %28x86 et x64%29</ProductName>
83+
<Install>true</Install>
84+
</BootstrapperPackage>
85+
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
86+
<Visible>False</Visible>
87+
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
88+
<Install>false</Install>
89+
</BootstrapperPackage>
90+
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
91+
<Visible>False</Visible>
92+
<ProductName>.NET Framework 3.5 SP1</ProductName>
93+
<Install>false</Install>
94+
</BootstrapperPackage>
95+
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
96+
<Visible>False</Visible>
97+
<ProductName>Windows Installer 3.1</ProductName>
98+
<Install>true</Install>
99+
</BootstrapperPackage>
100+
</ItemGroup>
101+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
102+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
103+
Other similar extension points exist, see Microsoft.Common.targets.
104+
<Target Name="BeforeBuild">
105+
</Target>
106+
<Target Name="AfterBuild">
107+
</Target>
108+
-->
109+
</Project>

Client/Client.csproj.user

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<PublishUrlHistory>publish\</PublishUrlHistory>
5+
<InstallUrlHistory />
6+
<SupportUrlHistory />
7+
<UpdateUrlHistory />
8+
<BootstrapperUrlHistory />
9+
<ErrorReportUrlHistory />
10+
<FallbackCulture>fr-FR</FallbackCulture>
11+
<VerifyUploadedFiles>false</VerifyUploadedFiles>
12+
</PropertyGroup>
13+
</Project>

Client/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using SharedAPI;
3+
4+
namespace Client
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
ClientLog.New();
11+
12+
try
13+
{
14+
Client.CreateTcpClient("127.177.177.177", 1717);
15+
Client.Connect("127.0.0.1", 80);
16+
while (true) Client.SendText(Log.Prompt("Send Text"));
17+
}
18+
catch (Exception exception)
19+
{
20+
Log.Exception(exception);
21+
}
22+
finally
23+
{
24+
Client.Close();
25+
Main(args);
26+
}
27+
}
28+
}
29+
}

Client/Properties/AssemblyInfo.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// Les informations générales relatives à un assembly dépendent de
5+
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
6+
// associées à un assembly.
7+
[assembly: AssemblyTitle("Client")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("Client")]
12+
[assembly: AssemblyCopyright("Copyright © 2021")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
17+
// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
18+
// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
19+
[assembly: ComVisible(false)]
20+
21+
// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
22+
[assembly: Guid("b65c0212-c72b-4bd4-b1e3-2c3e4d96694d")]
23+
24+
// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
25+
//
26+
// Version principale
27+
// Version secondaire
28+
// Numéro de build
29+
// Révision
30+
//
31+
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
32+
// en utilisant '*', comme indiqué ci-dessous :
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

Client/bin/Release/Client.exe

12 KB
Binary file not shown.

Client/bin/Release/Client.pdb

21.5 KB
Binary file not shown.

Client/bin/Release/Client.vshost.exe

11.3 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
8+
</requestedPrivileges>
9+
</security>
10+
</trustInfo>
11+
</assembly>

Client/bin/Release/SharedAPI.dll

6.5 KB
Binary file not shown.

Client/bin/Release/SharedAPI.pdb

21.5 KB
Binary file not shown.

Client/icon.ico

4.19 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
C:\Users\Abdelhakim\Desktop\TCP API\Client\bin\Release\Client.exe
2+
C:\Users\Abdelhakim\Desktop\TCP API\Client\bin\Release\Client.pdb
3+
C:\Users\Abdelhakim\Desktop\TCP API\Client\bin\Release\SharedAPI.dll
4+
C:\Users\Abdelhakim\Desktop\TCP API\Client\bin\Release\SharedAPI.pdb
5+
C:\Users\Abdelhakim\Desktop\TCP API\Client\obj\x86\Release\Client.csprojResolveAssemblyReference.cache
6+
C:\Users\Abdelhakim\Desktop\TCP API\Client\obj\x86\Release\Client.exe
7+
C:\Users\Abdelhakim\Desktop\TCP API\Client\obj\x86\Release\Client.pdb
Binary file not shown.

Client/obj/x86/Release/Client.exe

12 KB
Binary file not shown.

Client/obj/x86/Release/Client.pdb

21.5 KB
Binary file not shown.
Binary file not shown.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# C# .NET TCP/IP Socket API
2+
3+
Simple TCP Client and Server written in C# .NET for learning purposes only.
4+
5+
The project consists of:
6+
- Simple **Wrapper** for the C# .NET TCP/IP Socket API.
7+
- **Client** and **Server** console applications using it.

Server/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using SharedAPI;
3+
4+
namespace Server
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
ServerLog.New();
11+
12+
try
13+
{
14+
Server.CreateTcpListener("127.0.0.1", 80);
15+
Server.Start();
16+
while (true) { Server.Connect(); break; }
17+
while (true) Server.ReceiveText();
18+
}
19+
catch (Exception exception)
20+
{
21+
Log.Exception(exception);
22+
23+
}
24+
finally
25+
{
26+
Server.Stop();
27+
Main(args);
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)