using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ServerTest
{
public partial class Form1 : Form
{
private TcpListener Server;
private TcpClient SerClient;
private NetworkStream myStream;
private StreamWriter myWrite;
private Boolean Start = false;
private Thread myServer;
private int[] intArr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
intArr = new int[40];
for (int i = 0; i < intArr.Length; i++)
{
intArr[i] = i;
}
Start = true;
IPAddress addr = new IPAddress(0);
Server = new TcpListener(addr, 2007);
Server.Start();
myServer = new Thread(ServerStart);
myServer.Start();
//this.Timer.Enabled = true;
}
private void ServerStart()
{
if (txtMessage.InvokeRequired)
{
txtMessage.Invoke(new MessageViewHandler(MessageView), "server start");
}
while (Start)
{
try
{
SerClient = Server.AcceptTcpClient();
if (txtMessage.InvokeRequired)
{
txtMessage.Invoke(new MessageViewHandler(MessageView), "Client is connected");
}
else
MessageView("Client is connected");
myStream = SerClient.GetStream();
myWrite = new StreamWriter(myStream);
}
catch { }
}
}
private void MessageView(string strText)
{
this.txtMessage.AppendText(strText + "\r\n");
this.txtMessage.Focus();
this.txtMessage.ScrollToCaret();
}
private delegate void MessageViewHandler(string strText);
/*
private void Timer_Tick(object sender, EventArgs e)
{
Msg_send();
}
private void Msg_send()
{
try
{
string dt = Convert.ToString(DateTime.Now);
myWrite.WriteLine(dt);
myWrite.Flush();
}
catch { }
}
*/
private void btnSend_Click(object sender, EventArgs e)
{
byte[] size = new byte[64];
byte[] data = new byte[intArr.Length * 4];
size = BitConverter.GetBytes(intArr.Length);
myStream.Write(size, 0, size.Length);
for (int k = 0; k < intArr.Length; k++)
{
intToByte(intArr[k], data, k * 4);
}
/*
if (myStream != null && myWrite != null)
{
myWrite.WriteLine("서버에서 메시지를 보냅니다.");
myWrite.Flush();
}*/
myStream.Write(data, 0, data.Length);
}
public void intToByte(int value, byte[] data, int idx)
{
// int는 4byte이므로 4개의 byte에 기록 (int = 4byte = 32bit)
// 32bit를 24칸 쉬프팅을 하면 24비트는 짤리고 마지막 8bit(상위 1byte)에 값이 들어간다.
// 이것을 byte 캐스팅을 해버리면 8bit가 짤려서 저장된다.
// 나머지 24bit도 값을 밀어서 바이트(8비트)로 자른 뒤 집어넣는다.
// 즉 배열에 인덱스를 하나씩 증가시키면서 int값을 8bit씩 잘라서 집어넣는다.
// 마지막 배열에는 8비트만이 남으므로 쉬프트 연산으로 밀어버릴 필요가 없다.
data[idx] = (byte)(value >> 24);
data[++idx] = (byte)(value >> 16);
data[++idx] = (byte)(value >> 8);
data[++idx] = (byte)value;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!(myWrite == null))
{
try
{
myWrite.WriteLine("서버를 종료합니다");
myWrite.Flush();
}
catch
{ }
}
this.Start = false;
//this.Timer.Enabled = false;
if (!(myWrite == null))
{
myWrite.Close();
}
if (!(myStream == null))
{
myStream.Close();
}
if (!(SerClient == null))
{
SerClient.Close();
}
if (!(Server == null))
{
Server.Stop();
}
if (!(myServer == null))
{
myServer.Abort();
}
}
private void button1_Click(object sender, EventArgs e)
{
int i;
string strHostName;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (i = 0; i < addr.Length; i++)
{
txtMessage.Text += addr[i].ToString() + "\r\n";
/*
if (addr[i].AddressFamily.ToString() != "InterNetworkV6")
{
break;
}*/
}
//MessageBox.Show(addr[i].ToString());
}
}
}