C# socket
来源:互联网 发布:产品淘宝上线流程 编辑:程序博客网 时间:2024/05/10 03:22
- 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);
- 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();
- }
- }
- }
- 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=new IPEndPoint(serverIp,serverPort);
- byte[] byteMessage;
- Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
- socket.Connect(iep);
- byteMessage=Encoding.ASCII.GetBytes(textBox1.Text);
- socket.Send(byteMessage);
- socket.Shutdown(SocketShutdown.Both);
- socket.Close();
- }
- 基于TCP协议的发送和接收端
- TCP协议的接收端
- using System.Net.Sockets ;
- using System.Threading ;
- using System.IO ;
- int port = 8000;
- private Thread thThreadRead;
- private TcpListener tlTcpListen;
- private bool blistener = true;
- private NetworkStream nsStream;
- private StreamReader srRead;
- private System.Windows.Forms.StatusBar statusBar1;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.ListBox listBox1;
- private TcpClient tcClient ;
- private void Listen ( )
- {
- try
- {
- tlTcpListen = new TcpListener ( port ) ;
- tlTcpListen.Start ( ) ;
- statusBar1.Text = "正在监听" ;
- tcClient = tlTcpListen.AcceptTcpClient ( ) ;
- nsStream = tcClient.GetStream ( ) ;
- srRead=new StreamReader(nsStream);
- statusBar1.Text = "已经连接!";
- while( blistener )
- {
- string sMessage = srRead.ReadLine();
- if ( sMessage == "STOP" )
- {
- tlTcpListen.Stop();
- nsStream.Close();
- srRead.Close();
- statusBar1.Text = "连接已经关闭!" ;
- thThreadRead.Abort();
- return;
- }
- string sTime = DateTime.Now.ToShortTimeString ( ) ;
- listBox1.Items.Add ( sTime + " " + sMessage ) ;
- }
- }
- catch ( System.Security.SecurityException )
- {
- MessageBox.Show ( "侦听失败!" , "错误" ) ;
- }
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- thThreadRead = new Thread ( new ThreadStart ( Listen ) );
- thThreadRead.Start();
- button1.Enabled=false;
- }
- protected override void Dispose( bool disposing )
- {
- try
- {
- tlTcpListen.Stop();
- nsStream.Close();
- srRead.Close();
- thThreadRead.Abort();
- }
- catch{}
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- TCP协议的发送端
- using System.Net.Sockets;
- using System.Threading;
- using System.IO;
- using System.Net;
- private StreamWriter swWriter;
- private NetworkStream nsStream;
- private TcpClient tcpClient;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.TextBox textBox1;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.TextBox textBox2;
- private System.Windows.Forms.StatusBar statusBar1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private bool tcpConnect = false;
- private void button1_Click(object sender, System.EventArgs e)
- {
- IPAddress ipRemote ;
- try
- {
- ipRemote = IPAddress.Parse ( textBox1.Text ) ;
- }
- catch
- {
- MessageBox.Show ( "输入的IP地址不合法!" , "错误提示!" ) ;
- return ;
- }
- IPHostEntry ipHost ;
- try
- {
- ipHost = Dns.Resolve ( textBox1.Text ) ;
- }
- catch
- {
- MessageBox.Show ("远程主机不在线!" , "错误提示!" ) ;
- return ;
- }
- string sHostName = ipHost.HostName ;
- try
- {
- TcpClient tcpClient = new TcpClient(sHostName,8000);
- nsStream = tcpClient.GetStream();
- swWriter = new StreamWriter(nsStream);
- button1.Enabled = false ;
- button2.Enabled = true ;
- tcpConnect = true ;
- statusBar1.Text = "已经连接!" ;
- }
- catch
- {
- MessageBox.Show ( "无法和远程主机8000端口建立连接!" , "错误提示!" ) ;
- return ;
- }
- }
- private void button2_Click(object sender, System.EventArgs e)
- {
- if (textBox2.Text !="")
- {
- swWriter.WriteLine(textBox2.Text);
- swWriter.Flush();
- }
- else
- {
- MessageBox.Show("发送信息不能为空!","错误提示!");
- }
- }
- protected override void Dispose( bool disposing )
- {
- if ( tcpConnect )
- {
- swWriter.WriteLine ( "STOP" ) ;
- swWriter.Flush ();
- nsStream.Close ();
- swWriter.Close ();
- }
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }