Socket±à³Ì(C#)
//Socket»ù±¾±à³Ì
//·þÎñ¶Ë£º
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
Thread mythread ;
Socket socket;
// ÇåÀíËùÓÐÕýÔÚʹÓõÄ×ÊÔ´¡£
protected override void Dispose( bool disposing )
{
try
¡¡ { ¡¡¡¡
¡¡¡¡ socket.Close();//ÊÍ·Å×ÊÔ´
¡¡¡¡ mythread.Abort ( ) ;//ÖÐÖ¹Ïß³Ì
¡¡ }
¡¡ catch{ }
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
public static IPAddress GetServerIP()
{
IPHostEntry ieh=Dns.GetHostByName(Dns.GetHostName());
return ieh.AddressList[0];
}
private void BeginListen()
{
IPAddress ServerIp=GetServerIP();
IPEndPoint iep=new IPEndPoint(ServerIp,8000);
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
byte[] byteMessage=new byte[100];
this.label1.Text=iep.ToString();
socket.Bind(iep);
// do
while(true)
{
try
{
socket.Listen(5);
Socket newSocket=socket.Accept();
newSocket.Receive(byteMessage);
string sTime = DateTime.Now.ToShortTimeString ( ) ;
string msg=sTime+":"+"Message from:";
msg+=newSocket.RemoteEndPoint.ToString()+Encoding.Default.GetString(byteMessage);
this.listBox1.Items.Add(msg);
}
catch(SocketException ex)
{
this.label1.Text+=ex.ToString();
}
}
// while(byteMessage!=null);
}
//¿ªÊ¼¼àÌý
private void button1_Click(object sender, System.EventArgs e)
{
try
{
mythread = new Thread(new ThreadStart(BeginListen));
mythread.Start();
}
catch(System.Exception er)
{
MessageBox.Show(er.Message,"Íê³É",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
}
//¿Í»§¶Ë£º
using System.Net;
using System.Net.Sockets;
using System.Text;
private void button1_Click(object sender, System.EventArgs e)
{
BeginSend();
}
private void BeginSend()
{
string ip=this.txtip.Text;
string port=this.txtport.Text;
IPAddress serverIp=IPAddress.Parse(ip);
int serverPort=Convert.ToInt32(port);
IPEndPoint iep
Ïà¹ØÎĵµ£º
C#ÀࣺÀàÊÇC# Öй¦ÄÜ×îΪǿ´óµÄÊý¾ÝÀàÐÍ£¬ÀàÒ²¶¨ÒåÁËÊý¾ÝÀàÐ͵ÄÊý¾ÝºÍÐÐΪ¡£È»ºó£¬³ÌÐòÔ±¿ÉÒÔ´´½¨×÷Ϊ´ËÀàµÄʵÀýµÄ¶ÔÏó¡£
C#½á¹¹Ì壺½á¹¹ÌåÊÇÇáÁ¿Ð͵ÄÀ࣬ʹÓÿªÏúÏà¶ÔÓÚÀà½ÏС¡£
C#ÀàºÍC#½á¹¹ÌåÁ½ÕߵĹØÏµ£º
1.Êý¾ÝÀàÐÍ£ºÀàºÍ½á¹¹Ìå¶¼ÊÇÊý¾ÝÀàÐÍ£¬ÀàÊÇÒýÓÃÀàÐÍ£¬½á¹¹ÌåÊÇÖµÀàÐÍ¡£
2.¼Ì³ÐÐÔ£ºÀà¿ÉÒԼ̳кͱ»¼Ì³Ð£¬¶ø½á¹ ......
c#ÖлùÀࣨ¸¸ÀࣩÖеÄij·½·¨ÈôÏëÔÚÅÉÉúÀࣨ×ÓÀࣩÖб»ÖØÐ´£¨override£©£¬±ØÐ뽫»ùÀàÖеķ½·¨¶¨ÒåΪvirtual£¬¼´Ð麯Êý¡£
ÈôÅÉÉúÀཫ·½·¨ÐÞÊÎΪnew£¬¼´ÓÐÒâÒþ²Ø»ùÀàÖеķ½·¨¡£
ÏÂÃæ¿´Ò»×é´úÂ룺
public class Father
{
public void hand()
{
Console.WriteLine("Father.hand");
}
}
......
ÊôÐÔÊÇÀàÖпÉÒÔÏñÀàµÄ×Ö¶ÎÒ»Ñù·ÃÎʵķ½·¨¡£ÊôÐÔ¿ÉÒÔΪÀàµÄ×Ö¶ÎÌṩ±£»¤£¬±ÜÃâ×Ö¶ÎÔÚ¶ÔÏó²»ÖªµÀÇé¿öϱ»Ð޸ġ£C#ͨ¹ýÊôÐÔÀ´Ð޸쬶Áд»ò¼ÆËã˽ÓеÄ×ֶεÄÖµ¡£ÊôÐÔÏ൱ÓÚ¶Ô×ֶηÃÎʵķâ×°¡£ÏÂÀý×Ó¿ÉÒÔÇå³þ˵Ã÷Ó´£º
class Person
{
......
1.Òª¸ãÊý¾Ý¿â±à³Ì±ØÐëÒª¶®µÃÅäÖÃÊý¾Ý¿â,Óм¸µãÒªÌáµ½:
(1) ĬÈÏÇé¿öÏÂsql serverÊý¾Ý¿âÖ»ÔÊÐíÒÔwindowsÉí·ÝµÇ¼(¼´Ä¬ÈÏÄãÊǸõçÄÔµÄÖ÷ÈË,ÒÔÕâÖÖÉí·ÝµÇ¼¿É
ÒÔ¶ÔÊý¾Ý¿â·þÎñÆ÷ÓµÓÐ×î¸ßȨÏÞ),Äã¿ÉÒÔ¿ªÆôÆäSQL SERVER ºÍ windows»ìºÏµÇ¼ģʽ(¾Ísql server 2000¶ø
ÑÔÊÇÔÚÆóÒµ¹ÜÀíÆ÷ÀïµÄ²Ëµ¥À¸"²Ù×÷"-"ÊôÐÔ"Àï"°²È«ÐÔ" ......