using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace TCP_Test_Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("현재 클라이언트 컴퓨터의 IP : " + Utility.GetCurrentIPAddress());
// 클라이언트 소켓이 동작하는 스레드.
Thread clientThread = new Thread(ClientFunction);
clientThread.IsBackground = true;
clientThread.Start();
Console.WriteLine("현재 클라이언트 프로그램 실행중입니다. (종료하려면 아무 키나 누르세요...)");
Console.ReadLine();
}
private static void ClientFunction(object obj)
{
Console.WriteLine("현재 클라이언트 프로그램을 시작하겠습니다.");
using (Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("220.119.20.225"), 11200);
clientSocket.Connect(serverEndPoint);
int nTimes = 5;
while (nTimes-- > 0)
{
// 클라이언트 -> 서버 : 데이터 송신.
byte[] buf = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
Console.WriteLine(DateTime.Now.ToString());
clientSocket.Send(buf);
// 서버 -> 클라이언트 : 데이터 수신.
byte[] receivebytes = new byte[1024];
int nReceive = clientSocket.Receive(receivebytes);
string txt = Encoding.UTF8.GetString(receivebytes, 0, nReceive);
Console.WriteLine(txt);
Thread.Sleep(1000);
}
}
Console.WriteLine("TCP Client socket : Closed");
}
}
}
public static class Utility
{
public static IPAddress GetCurrentIPAddress()
{
IPAddress[] iPAddresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress iPAddress in iPAddresses)
{
if (iPAddress.AddressFamily == AddressFamily.InterNetwork)
{
return iPAddress;
}
}
return null;
}
}
서버측은 488 Page 그대로 구현했습니다.
책 488 Page 내용에 UDP에서는 5번 보냈길래 TCP 에서도 5번 보내니까 이런 오류가 뜨는데 이건 왜 이런건가요?
[최초 등록일: ]
[최종 수정일: 11/10/2018]