当前位置: 移动技术网 > IT编程>开发语言>.net > C#之Socket的简单使用

C#之Socket的简单使用

2019年02月13日  | 移动技术网IT编程  | 我要评论

医者风流txt,新歌排行榜2013,十爱嫂

socket是一种通信tcp/ip的通讯接口,也就是http的抽象层,就是socket在http之上,socket也就是发动机。实际上,传输层的tcp是基于网络层的ip协议的,而应用层的http协议又是基于传输层的tcp协议的,而socket本身不算是协议,就像上面所说,它只是提供了一个针对tcp或者udp编程的接口。在c#中可以非常方便的使用socket进行数据传输。

socket对象是c#使用它的重要对象在socket的构造函数中,我们可以设置它的地址,socket的类,支持的协议等等,其定义如下:

public socket(addressfamily addressfamily, sockettype sockettype, protocoltype protocoltype);

我们想要使用socket,那么就必须创建socket的对象,创建这个对象,就必须需要ipendpoint对象来绑定到套接词字中,有如下定义

// 创建负责监听的套接字,注意其中的参数;  
socketwatch = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
// 获得文本框中的ip对象;
ipaddress address = ipaddress.parse(textbox1.text.trim());
// 创建包含ip和端口号的网络节点对象;  
ipendpoint endpoint = new ipendpoint(address, int.parse(textbox2.text.trim()));

 然后再通过socket的bind来进行绑定。

socketwatch.bind(endpoint);

因为我们时刻会被内网中的其他ip和端口进行连接,那么我们就需要创建线程来进行观察,有如下定义

// 设置监听队列的长度;  
            socketwatch.listen(10);
            // 创建负责监听的线程;  
            threadwatch = new thread(watchconnecting);
            threadwatch.isbackground = true;
            threadwatch.start();
            showmsg("服务器启动监听成功!");

其检测方法如下,其中就是不断的去检测客户端的连接请求,通过accept()方法可以获取一个套接字,然后通过socket对象的remoteendpoint()可以获取一个ip。

void watchconnecting()
        {
            while (true)  // 持续不断的监听客户端的连接请求;  
            {
                // 开始监听客户端连接请求,accept方法会阻断当前的线程;  
                socket sokconnection = socketwatch.accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字;  
                // 想列表控件中添加客户端的ip信息;  
                online.items.add(sokconnection.remoteendpoint.tostring());
                // 将与客户端连接的 套接字 对象添加到集合中;  
                dict.add(sokconnection.remoteendpoint.tostring(), sokconnection);
                showmsg("客户端连接成功!");
                thread thr = new thread(recmsg);
                thr.isbackground = true;
                thr.start(sokconnection);
                dictthread.add(sokconnection.remoteendpoint.tostring(), thr);  //  将新建的线程 添加 到线程的集合中去。  
            }
        }

最后我们开启一个线程去执行recmsg方法,然后我们不停的去监听客户端给我们的数据发送。

void recmsg(object sokconnectionparn)
        {
            socket sokclient = sokconnectionparn as socket;
            while (true)
            {
                // 定义一个2m的缓存区;  
                byte[] arrmsgrec = new byte[1024 * 1024 * 2];
                // 将接受到的数据存入到输入  arrmsgrec中;  
                int length = -1;
                try
                {
                    length = sokclient.receive(arrmsgrec); // 接收数据,并返回数据的长度;  
                }
                catch (socketexception se)
                {
                    showmsg("异常:" + se.message);
                    // 从 通信套接字 集合中删除被中断连接的通信套接字;  
                    dict.remove(sokclient.remoteendpoint.tostring());
                    // 从通信线程集合中删除被中断连接的通信线程对象;  
                    dictthread.remove(sokclient.remoteendpoint.tostring());
                    // 从列表中移除被中断的连接ip  
                    online.items.remove(sokclient.remoteendpoint.tostring());
                    break;
                }
                catch (exception e)
                {
                    showmsg("异常:" + e.message);
                    // 从 通信套接字 集合中删除被中断连接的通信套接字;  
                    dict.remove(sokclient.remoteendpoint.tostring());
                    // 从通信线程集合中删除被中断连接的通信线程对象;  
                    dictthread.remove(sokclient.remoteendpoint.tostring());
                    // 从列表中移除被中断的连接ip  
                    online.items.remove(sokclient.remoteendpoint.tostring());
                    break;
                }
                if (arrmsgrec[0] == 0)  // 表示接收到的是数据;  
                {
                    string strmsg = system.text.encoding.utf8.getstring(arrmsgrec, 1, length - 1);// 将接受到的字节数据转化成字符串;  
                    showmsg(strmsg);
                }
                if (arrmsgrec[0] == 1) // 表示接收到的是文件;  
                {
                    savefiledialog sfd = new savefiledialog();

                    if (sfd.showdialog(this) == system.windows.forms.dialogresult.ok)
                    {// 在上边的 sfd.showdialog() 的括号里边一定要加上 this 否则就不会弹出 另存为 的对话框,而弹出的是本类的其他窗口,,这个一定要注意!!!【解释:加了this的sfd.showdialog(this),“另存为”窗口的指针才能被savefiledialog的对象调用,若不加thissavefiledialog 的对象调用的是本类的其他窗口了,当然不弹出“另存为”窗口。】  

                        string filesavepath = sfd.filename;// 获得文件保存的路径;  
                        // 创建文件流,然后根据路径创建文件;  
                        using (filestream fs = new filestream(filesavepath, filemode.create))
                        {
                            fs.write(arrmsgrec, 1, length - 1);
                            showmsg("文件保存成功:" + filesavepath);
                        }
                    }
                }
            }
        }

我们在方法中获得了一个object类型的对象,将这个object对象转换成了socket,然后我们通过socket的方法receive()方法接收返回的数据,其中里面有它的属性,可以获取ip还有一些数据等等。服务器向客户端发送数据也是非常简单。通过send方法就可以了,如以下定义:

string strmsg = "服务器" + "\r\n" + "   -->" + richtextbox1.text.trim() + "\r\n";
            byte[] arrmsg = system.text.encoding.utf8.getbytes(strmsg); // 将要发送的字符串转换成utf-8字节数组;  
            byte[] arrsendmsg = new byte[arrmsg.length + 1];
            arrsendmsg[0] = 0; // 表示发送的是消息数据  
            buffer.blockcopy(arrmsg, 0, arrsendmsg, 1, arrmsg.length);
            string strkey = "";
            strkey = online.text.trim();
            if (string.isnullorempty(strkey))   // 判断是不是选择了发送的对象;  
            {
                messagebox.show("请选择你要发送的好友!!!");
            }
            else
            {
                dict[strkey].send(arrsendmsg);// 解决了 sokconnection是局部变量,不能再本函数中引用的问题;  
                showmsg(strmsg);
                richtextbox1.clear();
            }

 最后需要注意的是,如果你的文件较大,有的时候这个缓冲区达不到你的文件字节那么大,那么就会截断,所以与的时候,先将文件转换为byte是正确的做法。只要在客户端进行逆转就可以了!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网