当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-03-22 13:54:00  来源:本站整理

<b>掌握台聊天程序实例代码</b>[VC/C++编程]

赞助商链接



  本文“<b>掌握台聊天程序实例代码</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

本日在网上找到了个掌握台聊天程序实例,感受不错,略微做些改正,以作珍藏之用.

服务器端程序代码:

view plaincopy to clipboardprint?
// 2009-06-28
#pragma comment(lib,"ws2_32.lib")
#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
string strCurMsg="";
void recvProc(SOCKET sockConnect)
{
char msgRcv[100]={0};
while(true)
{
if(SOCKET_ERROR==recv(sockConnect,msgRcv,sizeof(msgRcv),0))
{
cout<<"nHe is leaving";
return;
}
if(msgRcv[0]!=’’)
{
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="He said: ";
strCurMsg+=msgRcv;
strCurMsg+=’n’;
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
}
}
}
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion ) != 1) {
WSACleanup( );
return 1;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addrSrv;
memset(&addrSrv,0,sizeof(addrSrv));
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(5000);
if(bind(sockSrv,(sockaddr*)&addrSrv,sizeof(sockaddr))==SOCKET_ERROR)
{
cout<<"bind error"<<endl;
}
if(listen(sockSrv,5)==SOCKET_ERROR)
{
cout<<"listen error"<<endl;
}
SOCKADDR_IN addrClient;
int len=sizeof(sockaddr);
while(true)
{
cout<<"Waiting .....";
SOCKET sockConnect=accept(sockSrv,(sockaddr*)&addrClient,&len);
if(sockConnect==INVALID_SOCKET)
{
cout<<"invalid socket"<<endl;
return 0;
}
else
{
cout<<"He is comingn";
}
CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)recvProc,(void*)sockConnect,
0,NULL);
while(true)
{
char buf[100]={0};
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
cin.getline(buf,100);
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="You said:";
strCurMsg+=buf;
strCurMsg+=’n’;
send(sockConnect,buf,sizeof(buf),0);
}
closesocket(sockConnect);
}
WSACleanup();
return 0;
}

// 2009-06-28
#pragma comment(lib,"ws2_32.lib")
#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
string strCurMsg="";
void recvProc(SOCKET sockConnect)
{
char msgRcv[100]={0};
while(true)
{
if(SOCKET_ERROR==recv(sockConnect,msgRcv,sizeof(msgRcv),0))
{
cout<<"nHe is leaving";
return;
}
if(msgRcv[0]!=’’)
{
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="He said: ";
strCurMsg+=msgRcv;
strCurMsg+=’n’;
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
}
}
}
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion ) != 1) {
WSACleanup( );
return 1;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addrSrv;
memset(&addrSrv,0,sizeof(addrSrv));
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(5000);
if(bind(sockSrv,(sockaddr*)&addrSrv,sizeof(sockaddr))==SOCKET_ERROR)
{
cout<<"bind error"<<endl;
}
if(listen(sockSrv,5)==SOCKET_ERROR)
{
cout<<"listen error"<<endl;
}
SOCKADDR_IN addrClient;
int len=sizeof(sockaddr);
while(true)
{
cout<<"Waiting .....";
SOCKET sockConnect=accept(sockSrv,(sockaddr*)&addrClient,&len);
if(sockConnect==INVALID_SOCKET)
{
cout<<"invalid socket"<<endl;
return 0;
}
else
{
cout<<"He is comingn";
}
CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)recvProc,(void*)sockConnect,
0,NULL);
while(true)
{
char buf[100]={0};
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
cin.getline(buf,100);
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="You said:";
strCurMsg+=buf;
strCurMsg+=’n’;
send(sockConnect,buf,sizeof(buf),0);
}
closesocket(sockConnect);
}
WSACleanup();
return 0;
}
客户端程序代码:
view plaincopy to clipboardprint?
// 2009-06-28
#pragma comment(lib,"ws2_32.lib")
#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
string strCurMsg="";
void recvProc(SOCKET sockClient)
{
char msgRcv[100]={0};
while(true)
{
if(SOCKET_ERROR==recv(sockClient,msgRcv,sizeof(msgRcv),0))
{
cout<<"nHe is leaving";
return;
}
if(msgRcv[0]!=’’)
{
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="He said: ";
strCurMsg+=msgRcv;
strCurMsg+=’n’;
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
}
}
}

int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion ) != 1) {
WSACleanup( );
return 1;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(5000);
if (connect(sockClient,(sockaddr*)&addrSrv,sizeof(sockaddr)) == SOCKET_ERROR)
{
cout<<"connected failedn";
return 1;
}
else
{
cout<<"connect OKn";
}
CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)recvProc,(void*)sockClient,
0,NULL);
while(true)
{
char buf[100]={0};
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
cin.getline(buf,100);
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="You said:";
strCurMsg+=buf;
strCurMsg+=’n’;
send(sockClient,buf,sizeof(buf),0);
}
closesocket(sockClient);
WSACleanup();
return 0;
}
// 2009-06-28
#pragma comment(lib,"ws2_32.lib")
#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
string strCurMsg="";
void recvProc(SOCKET sockClient)
{
char msgRcv[100]={0};
while(true)
{
if(SOCKET_ERROR==recv(sockClient,msgRcv,sizeof(msgRcv),0))
{
cout<<"nHe is leaving";
return;
}
if(msgRcv[0]!=’’)
{
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="He said: ";
strCurMsg+=msgRcv;
strCurMsg+=’n’;
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
}
}
}
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion ) != 1) {
WSACleanup( );
return 1;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(5000);
if (connect(sockClient,(sockaddr*)&addrSrv,sizeof(sockaddr)) == SOCKET_ERROR)
{
cout<<"connected failedn";
return 1;
}
else
{
cout<<"connect OKn";
}
CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)recvProc,(void*)sockClient,
0,NULL);
while(true)
{
char buf[100]={0};
strCurMsg+="input: ";
system("cls");
cout<<strCurMsg;
cin.getline(buf,100);
strCurMsg.erase(strCurMsg.end()-7,strCurMsg.end());
strCurMsg+="You said:";
strCurMsg+=buf;
strCurMsg+=’n’;
send(sockClient,buf,sizeof(buf),0);
}
closesocket(sockClient);
WSACleanup();
return 0;
}


  以上是“<b>掌握台聊天程序实例代码</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .