socket

  C#socket例子 收藏
服务端:

view plaincopy to clipboardprint?
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Net.Sockets;  
using System.Threading;  
using System.Collections;  
using System.IO;  
using System.Net;  
using System.Data;  
using System.ComponentModel;  
 
namespace ConsoleApplication33  
 
    class Program  
    
        Socket listener = null;  
 
        static void Main(string[] args)  
        
            int port = 3388;  
            IPAddress ipAddress = IPAddress.Parse("192.160.9.234");  
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);  
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
 
            try 
            
                listener.Bind(localEndPoint);  
                listener.Listen(5);  
 
                while (true)  
                
                    Socket handler = listener.Accept();  
                    Console.WriteLine("连接成功");  
                    Thread myThread = new Thread(new ParameterizedThreadStart(SocketThread));  
                    myThread.Start(handler);  
                
            
            catch (Exception e)  
            
                Console.WriteLine(e.ToString());  
            
 
        
 
        static void SocketThread(object argSocket)  
        
            Socket rev = (Socket)argSocket;  
            byte[] bytData = new byte[1024];  
            int i = 0;  
 
            while (i < 20)  
            
                int receivedLength = rev.Receive(bytData);  
                string strText = System.Text.Encoding.UTF8.GetString(bytData, 0, receivedLength);  
                Console.WriteLine(strText);  
                i++;  
            
        
    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.IO;
using System.Net;
using System.Data;
using System.ComponentModel;

namespace ConsoleApplication33
{
    class Program
    {
        Socket listener = null;

        static void Main(string[] args)
        {
            int port = 3388;
            IPAddress ipAddress = IPAddress.Parse("192.160.9.234");
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(5);

                while (true)
                {
                    Socket handler = listener.Accept();
                    Console.WriteLine("连接成功");
                    Thread myThread = new Thread(new ParameterizedThreadStart(SocketThread));
                    myThread.Start(handler);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

        static void SocketThread(object argSocket)
        {
            Socket rev = (Socket)argSocket;
            byte[] bytData = new byte[1024];
            int i = 0;

            while (i < 20)
            {
                int receivedLength = rev.Receive(bytData);
                string strText = System.Text.Encoding.UTF8.GetString(bytData, 0, receivedLength);
                Console.WriteLine(strText);
                i++;
            }
        }
    }
}
 

客户端:

view plaincopy to clipboardprint?
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading;  
using System.Net.Sockets;  
using System.Net;  
 
namespace ConsoleApplication34  
 
    class Program  
    
        private Socket sender = null;  
 
        static void Main(string[] args)  
        
            try 
            
                int port = 3388;  
                IPAddress ipAddress = IPAddress.Parse("192.160.9.234");  
                IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port);  
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
                sender.Connect(remoteEndPoint);  
                string strSend = "process a";  
                byte [] bytSend = Encoding.UTF8.GetBytes(strSend);  
 
                for (int i = 0; i < 20; i++)  
                
                    sender.Send(bytSend);  
                    Thread.Sleep(1000);  
                
                sender.Close();  
            
            catch (Exception e)  
            
                Console.WriteLine(e.ToString());  
            
        
    

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/houpk345/archive/2010/05/21/5613819.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值