Skip to content

Commit

Permalink
클라이언트 수정 및 Echo 클라이언트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jacking75 committed Nov 28, 2023
1 parent 1991795 commit acb5910
Show file tree
Hide file tree
Showing 22 changed files with 2,049 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Tutorials/EchoClient/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
93 changes: 93 additions & 0 deletions Tutorials/EchoClient/ClientSimpleTcp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Net.Sockets;
using System.Net;

namespace csharp_test_client
{
public class ClientSimpleTcp
{
public Socket Sock = null;
public string LatestErrorMsg;


//소켓연결
public bool Connect(string ip, int port)
{
try
{
IPAddress serverIP = IPAddress.Parse(ip);
int serverPort = port;

Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Sock.Connect(new IPEndPoint(serverIP, serverPort));

if (Sock == null || Sock.Connected == false)
{
return false;
}

return true;
}
catch (Exception ex)
{
LatestErrorMsg = ex.Message;
return false;
}
}

public Tuple<int,byte[]> Receive()
{

try
{
byte[] ReadBuffer = new byte[2048];
var nRecv = Sock.Receive(ReadBuffer, 0, ReadBuffer.Length, SocketFlags.None);

if (nRecv == 0)
{
return null;
}

return Tuple.Create(nRecv,ReadBuffer);
}
catch (SocketException se)
{
LatestErrorMsg = se.Message;
}

return null;
}

//스트림에 쓰기
public void Send(byte[] sendData)
{
try
{
if (Sock != null && Sock.Connected) //연결상태 유무 확인
{
Sock.Send(sendData, 0, sendData.Length, SocketFlags.None);
}
else
{
LatestErrorMsg = "먼저 채팅서버에 접속하세요!";
}
}
catch (SocketException se)
{
LatestErrorMsg = se.Message;
}
}

//소켓과 스트림 닫기
public void Close()
{
if (Sock != null && Sock.Connected)
{
//Sock.Shutdown(SocketShutdown.Both);
Sock.Close();
}
}

public bool IsConnected() { return (Sock != null && Sock.Connected) ? true : false; }
}
}
69 changes: 69 additions & 0 deletions Tutorials/EchoClient/DevLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Runtime.CompilerServices;
using System.Threading;

namespace csharp_test_client
{
public class DevLog
{
static System.Collections.Concurrent.ConcurrentQueue<string> logMsgQueue = new System.Collections.Concurrent.ConcurrentQueue<string>();

static Int64 출력가능_로그레벨 = (Int64)LOG_LEVEL.TRACE;



static public void Init(LOG_LEVEL logLevel)
{
ChangeLogLevel(logLevel);
}

static public void ChangeLogLevel(LOG_LEVEL logLevel)
{
Interlocked.Exchange(ref 출력가능_로그레벨, (int)logLevel);
}

public static LOG_LEVEL CurrentLogLevel()
{
var curLogLevel = (LOG_LEVEL)Interlocked.Read(ref 출력가능_로그레벨);
return curLogLevel;
}

static public void Write(string msg, LOG_LEVEL logLevel = LOG_LEVEL.TRACE,
[CallerFilePath] string fileName = "",
[CallerMemberName] string methodName = "",
[CallerLineNumber] int lineNumber = 0)
{
if (CurrentLogLevel() <= logLevel)
{
logMsgQueue.Enqueue(string.Format("{0}:{1}| {2}", DateTime.Now, methodName, msg));
}
}

static public bool GetLog(out string msg)
{
if (logMsgQueue.TryDequeue(out msg))
{
return true;
}

return false;
}

}


public enum LOG_LEVEL
{
TRACE,
DEBUG,
INFO,
WARN,
ERROR,
DISABLE
}
}
Loading

0 comments on commit acb5910

Please sign in to comment.