Skip to content

Commit acb5910

Browse files
committed
클라이언트 수정 및 Echo 클라이언트 추가
1 parent 1991795 commit acb5910

22 files changed

+2049
-12
lines changed

Tutorials/EchoClient/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Net.Sockets;
3+
using System.Net;
4+
5+
namespace csharp_test_client
6+
{
7+
public class ClientSimpleTcp
8+
{
9+
public Socket Sock = null;
10+
public string LatestErrorMsg;
11+
12+
13+
//소켓연결
14+
public bool Connect(string ip, int port)
15+
{
16+
try
17+
{
18+
IPAddress serverIP = IPAddress.Parse(ip);
19+
int serverPort = port;
20+
21+
Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
22+
Sock.Connect(new IPEndPoint(serverIP, serverPort));
23+
24+
if (Sock == null || Sock.Connected == false)
25+
{
26+
return false;
27+
}
28+
29+
return true;
30+
}
31+
catch (Exception ex)
32+
{
33+
LatestErrorMsg = ex.Message;
34+
return false;
35+
}
36+
}
37+
38+
public Tuple<int,byte[]> Receive()
39+
{
40+
41+
try
42+
{
43+
byte[] ReadBuffer = new byte[2048];
44+
var nRecv = Sock.Receive(ReadBuffer, 0, ReadBuffer.Length, SocketFlags.None);
45+
46+
if (nRecv == 0)
47+
{
48+
return null;
49+
}
50+
51+
return Tuple.Create(nRecv,ReadBuffer);
52+
}
53+
catch (SocketException se)
54+
{
55+
LatestErrorMsg = se.Message;
56+
}
57+
58+
return null;
59+
}
60+
61+
//스트림에 쓰기
62+
public void Send(byte[] sendData)
63+
{
64+
try
65+
{
66+
if (Sock != null && Sock.Connected) //연결상태 유무 확인
67+
{
68+
Sock.Send(sendData, 0, sendData.Length, SocketFlags.None);
69+
}
70+
else
71+
{
72+
LatestErrorMsg = "먼저 채팅서버에 접속하세요!";
73+
}
74+
}
75+
catch (SocketException se)
76+
{
77+
LatestErrorMsg = se.Message;
78+
}
79+
}
80+
81+
//소켓과 스트림 닫기
82+
public void Close()
83+
{
84+
if (Sock != null && Sock.Connected)
85+
{
86+
//Sock.Shutdown(SocketShutdown.Both);
87+
Sock.Close();
88+
}
89+
}
90+
91+
public bool IsConnected() { return (Sock != null && Sock.Connected) ? true : false; }
92+
}
93+
}

Tutorials/EchoClient/DevLog.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using System.Runtime.CompilerServices;
8+
using System.Threading;
9+
10+
namespace csharp_test_client
11+
{
12+
public class DevLog
13+
{
14+
static System.Collections.Concurrent.ConcurrentQueue<string> logMsgQueue = new System.Collections.Concurrent.ConcurrentQueue<string>();
15+
16+
static Int64 출력가능_로그레벨 = (Int64)LOG_LEVEL.TRACE;
17+
18+
19+
20+
static public void Init(LOG_LEVEL logLevel)
21+
{
22+
ChangeLogLevel(logLevel);
23+
}
24+
25+
static public void ChangeLogLevel(LOG_LEVEL logLevel)
26+
{
27+
Interlocked.Exchange(ref 출력가능_로그레벨, (int)logLevel);
28+
}
29+
30+
public static LOG_LEVEL CurrentLogLevel()
31+
{
32+
var curLogLevel = (LOG_LEVEL)Interlocked.Read(ref 출력가능_로그레벨);
33+
return curLogLevel;
34+
}
35+
36+
static public void Write(string msg, LOG_LEVEL logLevel = LOG_LEVEL.TRACE,
37+
[CallerFilePath] string fileName = "",
38+
[CallerMemberName] string methodName = "",
39+
[CallerLineNumber] int lineNumber = 0)
40+
{
41+
if (CurrentLogLevel() <= logLevel)
42+
{
43+
logMsgQueue.Enqueue(string.Format("{0}:{1}| {2}", DateTime.Now, methodName, msg));
44+
}
45+
}
46+
47+
static public bool GetLog(out string msg)
48+
{
49+
if (logMsgQueue.TryDequeue(out msg))
50+
{
51+
return true;
52+
}
53+
54+
return false;
55+
}
56+
57+
}
58+
59+
60+
public enum LOG_LEVEL
61+
{
62+
TRACE,
63+
DEBUG,
64+
INFO,
65+
WARN,
66+
ERROR,
67+
DISABLE
68+
}
69+
}

0 commit comments

Comments
 (0)