日期:2014-05-18 浏览次数:20815 次
//设定地址 m_socketAddr.sin_addr.s_addr=htonl(INADDR_ANY);//IP地址 m_socketAddr.sin_family=AF_INET;//采样TCP/IP协议簇 m_socketAddr.sin_port=8001;//htons(8001);端口号 m_addLength=sizeof(m_socketAddr);//sockaddr_in结构体大小 //创建socket //AF_INET(同PF_INET):采样TCP/IP协议簇 //SOCK_STREAM:流套接字类型;SOCK_DGRAM:数据报套接字 //最后一个是通讯协议,可以不指定,设置为0. m_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //绑定 if (bind(m_socket,(sockaddr*)&m_socketAddr,m_addLength)) { GetDlgItem(IDC_STATIC_INFO)->SetWindowText("绑定失败!"); return; } else { //m_list.InsertItem(count++,inet_ntoa(serv.sin_addr)); GetDlgItem(IDC_STATIC_INFO)->SetWindowText("服务器创建成功!"); GetDlgItem(IDC_BTN_RUN)->EnableWindow(false); GetDlgItem(IDC_BTN_SEND)->EnableWindow(true); GetDlgItem(IDC_BTN_STOP)->EnableWindow(true); //开始侦听 //第二个参数请求队列的最大数量 listen(m_socket,5); //调用线程 m_threadStopFlag=true; AfxBeginThread(StartServerThread, this); }
IPAddress ip = IPAddress.Parse("192.168.1.91"); IPEndPoint ipe = new IPEndPoint(ip, 8001); Socket m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.Connect(ipe);