- °£´ÜÇÑ TCP & UDP Åë½Å
- ÆÄÀÏ À̸§, È®ÀåÀÚ µî ºÐ¸®Çϱâ
- OS Á¾·ù ¾Ë¾Æ³»±â
- ½Ã½ºÅÛ Æú´õ °æ·Î ¾Ë¾Æ³»±â
- ½ÇÇà ÆÄÀÏ °æ·Î ¾Ë¾Æ³»±â
.NET TipÀ̶ó°í ºÒ·¯¾ß Çϳª?
1 °£´ÜÇÑ TCP & UDP Åë½Å
public static string TcpSendReceive(string ipStr, int port, string sendMsg, int timeout)
{
Socket tcpSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp
);
tcpSocket.ReceiveTimeout = timeout;
StringBuilder result = new StringBuilder();
tcpSocket.Connect(ipStr, port);
if (tcpSocket.Connected)
{
try
{
Byte[] sendBuffer = Encoding.ASCII.GetBytes(sendMsg);
tcpSocket.Send(sendBuffer, sendBuffer.Length, 0);
Byte[] recvBuffer = new Byte[1024];
Int32 bytes = tcpSocket.Receive(recvBuffer, recvBuffer.Length, 0);
result.Append(Encoding.ASCII.GetString(recvBuffer, 0, bytes));
while (bytes > 0)
{
bytes = tcpSocket.Receive(recvBuffer, recvBuffer.Length, 0);
result.Append(Encoding.ASCII.GetString(recvBuffer, 0, bytes));
}
}
catch (System.Net.Sockets.SocketException)
{
result.Append("Error");
}
tcpSocket.Close();
}
else
{
result.Append("Error");
}
return result.ToString();
}
public static string UdpSendReceive(string ipStr, int port, string sendMsg, int timeout)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipStr), port);
Socket udpSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp
);
udpSocket.ReceiveTimeout = timeout;
string result = "";
Byte[] sendBuffer = Encoding.ASCII.GetBytes(sendMsg);
udpSocket.SendTo(sendBuffer, sendBuffer.Length, SocketFlags.None, ip);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remote = (EndPoint)sender;
Byte[] recvBuffer = new byte[1024];
try
{
int bytes = udpSocket.ReceiveFrom(recvBuffer, ref remote);
if (bytes > 0)
result = Encoding.ASCII.GetString(recvBuffer, 0, bytes);
}
catch (System.Net.Sockets.SocketException)
{
result = "Error";
}
udpSocket.Close();
return result;
} ºí·ÏÅ· ¼ÒÄÏ ¸¸¼¼~
2 ÆÄÀÏ À̸§, È®ÀåÀÚ µî ºÐ¸®Çϱâ
3 OS Á¾·ù ¾Ë¾Æ³»±â
OperatingSystem os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());
4 ½Ã½ºÅÛ Æú´õ °æ·Î ¾Ë¾Æ³»±â
Environment.GetFolderPath( Environment.SpecialFolder.Personal )
5 ½ÇÇà ÆÄÀÏ °æ·Î ¾Ë¾Æ³»±â
Application.ExecutablePath or Application.StartupPath
¾Æ´Ï¸é ÀÌ·¸°Ôµµ...
System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0).FullyQualifiedName
System.Reflection.Assembly.GetExecutingAssembly.Location
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)