-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
2,049 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.