그러냐

C# Socket 클레스를 이용한 바이트단위 Socket 프로그램 본문

c#

C# Socket 클레스를 이용한 바이트단위 Socket 프로그램

관절분리 2016. 1. 28. 11:22
반응형

* Socket 클레스를 이용한 바이트단위 Socket 프로그램*

# Server--------------------------------------------------------------------
1. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 7000);
2. Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
3. server.Bind(ipep);
4. server.Listen(10);
5. Socket client = server.Accept();
# Client--------------------------------------------------------------------
1. IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), 7000);
2. Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
3. client.Connect(ipep);
* NetworkStream 클래스를 이용한 문자열 기반 Socket 프로그램*
# Server--------------------------------------------------------------------
1. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 7000);
2. Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
3. server.Bind(ipep);
4. server.Listen(10);
5. Socket client = server.Accept();
6. NetworkStream stream = new NetworkStream(client);
7. StreamReader sr = new StreamReader(stream);
8. StreamWriter sw = new StreamWriter(stream);
9. string msg = sr.ReadLine();
0. sw.WriteLine(); sw.Flush();
# Client--------------------------------------------------------------------
1. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 7000);
2. Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
3. client.Connect(ipep);
4. NetworkStream stream = new NetworkStream(client);
5. StreamReader sr = new StreamReader(stream);
6. StreamWriter sw = new StreamWriter(stream);
7. string msg = sr.ReadLine();
8. sw.WriteLine(); sw.Flush();
* C# 헬퍼 클래스를 이용한 Socket 프로그램*
# Server--------------------------------------------------------------------
1. TcpListener server = new TcpListener(7000);
2. server.Start();
3. TcpClient client = server.AcceptTcpClient();
4. NetworkStream stream = client.GetStream();
5. StreamReader sr = new StreamReader(stream);
6. StreamWriter sw = new StreamWriter(stream);
7. string msg = sr.ReadLine();
8. sw.WriteLine(); sw.Flush();
# Client--------------------------------------------------------------------
1. TcpClient client = new TcpClient(ip, 7000);
2. NetworkStream stream = client.GetStream();
3. StreamReader sr = new StreamReader(stream);
4. StreamWriter sw = new StreamWriter(stream);
5. string msg = sr.ReadLine();
6. sw.WriteLine(); sw.Flush();
C#기반의 소켓통신 프로그래밍에 대한 기본적인 코드를 볼수있다.
반응형