StoryCode

Console Program, 자동 창 닫기 보류

C#/기타
반응형

1) Console.ReadLine();


2) Console.ReadKey();


3) 실행시 Ctrl + F5


4) System.Threading.Thread.Sleep(1000);

반응형

'C# > 기타' 카테고리의 다른 글

Socket Client/ Server 샘플 예제  (0) 2018.05.23
Audio Live Streaming  (0) 2018.05.23
디버깅시 코드에 브레이크 코드 넣기  (0) 2018.05.22

Socket Client/ Server 샘플 예제

C#/기타
반응형
// Server Socket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// NameSpace 선언
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace ServerSideSocket
{
  class ServerClass
  {
    public static Socket Server , Client;
     
    public static byte[] getByte = new byte[1024];
    public static byte[] setByte = new byte[1024];
     
    public const int sPort = 5000;
     
    [STAThread]
    static void Main(string[] args)
    {
      string stringbyte = null;
      IPAddress serverIP = IPAddress.Parse("127.0.0.1");
      IPEndPoint serverEndPoint = new IPEndPoint(serverIP,sPort);
       
      try
      {     
        Server= new Socket(
          AddressFamily.InterNetwork,
          SocketType.Stream,ProtocolType.Tcp);
           
        Server.Bind(serverEndPoint);
        Server.Listen(10);
 
        Console.WriteLine("------------------------");
        Console.WriteLine("클라이언트의 연결을 기다립니다. ");
        Console.WriteLine("------------------------");
                   
        Client = Server.Accept();
   
        if(Client.Connected)
        {
          while(true)
          {
            Client.Receive(getByte,0,getByte.Length,SocketFlags.None);
            stringbyte = Encoding.UTF7.GetString(getByte);
 
            if (stringbyte != String.Empty)
            {
              int getValueLength = 0;
              getValueLength = byteArrayDefrag(getByte);
               
              stringbyte = Encoding.UTF7.GetString(
                getByte,0,getValueLength+1);
 
              Console.WriteLine("수신데이터:{0} | 길이:{1}",
                stringbyte,getValueLength+1);
                 
              setByte = Encoding.UTF7.GetBytes(stringbyte);
              Client.Send(setByte,0,setByte.Length,SocketFlags.None);
            }
           
            getByte = new byte[1024];
            setByte = new byte[1024];
          }
        }
      }
        catch(System.Net.Sockets.SocketException socketEx)
      {
        Console.WriteLine("[Error]:{0}", socketEx.Message);
      }
      catch(System.Exception commonEx)
      {
        Console.WriteLine("[Error]:{0}", commonEx.Message);
      }
      finally
      {
        Server.Close();
        Client.Close();
      }
    }
     
    public static int byteArrayDefrag(byte[] sData)
    {
      int endLength = 0;
       
      for(int i = 0; i < sData.Length; i++)
      {
        if((byte)sData[i] != (byte)0)
        {
          endLength = i;
        }
      }
       
      return endLength;
    }
  }
}


// Client Socket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace ClientSideSocket
{
  class ClientClass
  {
    public static Socket socket;
    public static byte[] getbyte = new byte[1024];
    public static byte[] setbyte = new byte[1024];
 
    public const int sPort = 5000;
 
    [STAThread]
    static void Main(string[] args)
    {
      string sendstring = null;
      string getstring = null;
 
      IPAddress serverIP = IPAddress.Parse("127.0.0.1");
      IPEndPoint serverEndPoint = new IPEndPoint(serverIP,sPort);
 
      socket = new Socket(
        AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
         
      Console.WriteLine("------------------------------");
      Console.WriteLine(" 서버로 접속합니다.[엔터를 입력하세요] ");
      Console.WriteLine("------------------------------");
      Console.ReadLine();
 
      socket.Connect(serverEndPoint);
 
      if (socket.Connected)
      {
        Console.WriteLine(">>연결 되었습니다.(데이터를 입력하세요)");
      }
 
      while(true)
      {
        Console.Write(">>");
        sendstring = Console.ReadLine();
         
        if(sendstring != String.Empty)
        {
          int getValueLength = 0;
          setbyte = Encoding.UTF7.GetBytes(sendstring);
           
          socket.Send(setbyte,0,
            setbyte.Length,SocketFlags.None);
           
          Console.WriteLine("송신 데이터 : {0} | 길이{1}",
            sendstring, setbyte.Length);
             
          socket.Receive(getbyte,0,
            getbyte.Length,SocketFlags.None);
             
          getValueLength = byteArrayDefrag(getbyte);
           
          getstring = Encoding.UTF7.GetString(getbyte,
            0,getValueLength+1);
           
          Console.WriteLine(">>수신된 데이터 :{0} | 길이{1}",
            getstring , getValueLength+1);
        }
         
        getbyte = new byte[1024];
       }
    }
     
    public static int byteArrayDefrag(byte[] sData)
    {
      int endLength = 0;
       
      for(int i = 0; i < sData.Length; i++)
      {
        if((byte)sData[i] != (byte)0)
        {
          endLength = i;
        }
      }
       
      return endLength;
    }
  }
}



[출처] https://hooni.net/2382



반응형

'C# > 기타' 카테고리의 다른 글

Console Program, 자동 창 닫기 보류  (0) 2018.05.29
Audio Live Streaming  (0) 2018.05.23
디버깅시 코드에 브레이크 코드 넣기  (0) 2018.05.22

Audio Live Streaming

C#/기타
반응형

쉽게 되는 방법이 있군요.



반응형

'C# > 기타' 카테고리의 다른 글

Console Program, 자동 창 닫기 보류  (0) 2018.05.29
Socket Client/ Server 샘플 예제  (0) 2018.05.23
디버깅시 코드에 브레이크 코드 넣기  (0) 2018.05.22

디버깅시 코드에 브레이크 코드 넣기

C#/기타
반응형

System.Diagnostics.Debugger.Break();

반응형

'C# > 기타' 카테고리의 다른 글

Console Program, 자동 창 닫기 보류  (0) 2018.05.29
Socket Client/ Server 샘플 예제  (0) 2018.05.23
Audio Live Streaming  (0) 2018.05.23