C/S模式的socket套接字编程例子

来源:互联网 发布:电力预算软件 编辑:程序博客网 时间:2024/05/13 04:57
客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Net;

namespace socketC
{
    
class SocketC
    
{
        
static void Main(string[] args)
        
{
            
try
            
{
                
int port = 2000;//终端端口号

                
string host = "192.168.1.27";//终端IP

                IPAddress ip 
= IPAddress.Parse(host);

                IPEndPoint ipe 
= new IPEndPoint(ip, port);

                Socket c 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立一个Socket实例

                Console.WriteLine(
"Connecting...");

                c.Connect(ipe);
//连接终端

                
string sendStr = "hello,this is a socket test";//发送给终端的消息

                
byte[] bs = Encoding.ASCII.GetBytes(sendStr);//转换成字节

                Console.WriteLine(
"Send Message...");

                c.Send(bs, bs.Length, 
0);//发送消息

                
string recvStr = "";//从终端返回的消息

                
byte[] recvBytes = new byte[1024];

                
int bytes = c.Receive(recvBytes, recvBytes.Length, 0);//接收终端返回的消息,保存在recvBytes数组中

                recvStr 
+= Encoding.ASCII.GetString(recvBytes, 0, bytes);

                Console.WriteLine(
"Client get message:{0}", recvStr);

                c.Close();
//关闭Socket
            }

            
catch(ArgumentNullException e)
            
{
                Console.WriteLine(
"ArgumentNullException:{0}", e);
            }

            
catch(SocketException e)
            
{
                Console.WriteLine(
"SocketException:{0}", e);
            }

            Console.WriteLine(
"Press Enter to Exit");
            
            Console.ReadLine();
        }

    }

}

 服务器端:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace socketS
{
    
class SocketS
    
{
        
static void Main(string[] args)
        
{
            
try
            
{
                
int port = 2000;//终端端口号

                
string host = "192.168.1.27";

                IPAddress ip 
= IPAddress.Parse(host);//终端IP

                IPEndPoint ipe 
= new IPEndPoint(ip, port);//建立通信终端

                Socket s 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket对象

                s.Bind(ipe);
//绑定通讯终端

                s.Listen(
0);//终端开始监听是否有连接请求

                Console.WriteLine(
"Wait for connect...");

                Socket temp 
= s.Accept();//如果有新的连接请求,为该请求创建一个新的Socket实例

                Console.WriteLine(
"Get a connect");

                
string recvStr = "";//从客户端接收的信息

                
byte[] recvBytes = new byte[1024];

                
int bytes;//接收到的信息的字节数

                bytes 
= temp.Receive(recvBytes, recvBytes.Length, 0);//接收信息,保存在recvBytes数组中

                recvStr 
+= Encoding.ASCII.GetString(recvBytes, 0, bytes);

                Console.WriteLine(
"Server Get Message:{0}", recvStr);//转换成字符串

                
string sendStr = "OK!Client send message successfully";//发送给客户端的信息

                
byte[] bs = Encoding.ASCII.GetBytes(sendStr);//转换成字节

                temp.Send(bs, bs.Length, 
0);//发送信息

                temp.Close();
//关闭Socket

                s.Close();
            }

            
catch (ArgumentNullException e)
            
{
                Console.WriteLine(
"ArgumentNullException:{0}", e);
            }

            
catch (SocketException e)
            
{
                Console.WriteLine(
"SocketException:{0}", e);
            }

            Console.WriteLine(
"Press Enter to Exit");

            Console.ReadLine();
        }

    }

}

下面教大家怎么使用,假设用你自己的机器当服务器端,把你的IP设为192.168.1.27.或者不改你的IP,把程序中的IP改为你的IP.

然后将上面的两个类编译成可执行文件.假设你把上面两个类保存在了D盘下面.然后: 开始--所有程序--Microsoft Visual Studio 2005--Visual Studio Tools--Visual Studio 2005命令提示.打开了命令提示窗口,然后输入:

csc D:/SocketC.cs 回车

再输入csc D:/SocketS.cs 回车.

就把上面两个类生成了.exe文件了.这两个exe文件默认放在了VS2005目录下面的VC文件夹里.你找到这两个exe文件.然后双击就可以运行.

下一步就是测试Socket通信.

假设你的机器是服务器.然后在你的机器上运行SocketS.exe.然后将SocketC.exe发给另外一个人,在他的机器上运行.

就会显示程序中提示的信息了.

 

原创粉丝点击