#include <stdio.h>
#include <stdio.h>老前谨
#include <winsock.h>
#pragma comment(lib,"Ws2_32")
#define PORT 6666 /* 客户机连接远程主机的端口 */
#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */
int main()
{
int sockfd, numbytes
char buf[MAXDATASIZE]
char msg[MAXDATASIZE]
char *argv="127.0.0.1"
struct sockaddr_in their_addr /* 对方的地址端口信息 */
WSADATA wsWSAStartup(MAKEWORD(2,2),&ws)//初始化Windows Socket Dll
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//如果建立socket失败,退出程序
printf("socket error\n")
exit(1)
}
//连接悔洞对方
their_addr.sin_family = AF_INET/* 协议类型是INET */
their_addr.sin_port = htons(PORT) /* 连接对方PORT端口 */
their_addr.sin_addr.s_addr = inet_addr(argv) /* 连接对方的IP */
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
{
//如果连接失败,退出程序
printf("connet error\n")
closesocket(sockfd)
exit(1)
}
while(1){
scanf("%s",msg)
//发送数据
if (send(sockfd, msg, MAXDATASIZE, 0) == -1)
{
printf("send error")
closesocket(sockfd)
exit(1)
}
//接收数据,侍基并打印出来
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
//接收数据失败,退出程序
printf("recv error\n")
closesocket(sockfd)
exit(1)
}
buf[numbytes] = '\0'
printf("Received: %s\n",buf)}
closesocket(sockfd)
return 0
}
SERVER:
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib,"Ws2_32")
#define MYPORT 6666 /*定义用户连接端口*/
#define BACKLOG 10 /*多少等待连接控制*/ #define MAXDATASIZE 100
int main()
{
int sockfd, new_fd /*定义套接字*/
struct sockaddr_in my_addr /*本地地址信息 */
struct sockaddr_in their_addr /*连接者地址信息*/
int sin_size,numbytes char msg[10],buf[MAXDATASIZE]
WSADATA ws
WSAStartup(MAKEWORD(2,2),&ws) //初始化Windows Socket Dll
//建立socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//如果建立socket失败,退出程序
printf("socket error\n")
exit(1)
}
//bind本机的MYPORT端口
my_addr.sin_family = AF_INET/* 协议类型是INET */
my_addr.sin_port = htons(MYPORT) /* 绑定MYPORT端口*/
my_addr.sin_addr.s_addr = INADDR_ANY /* 本机IP*/
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
//bind失败,退出程序
printf("bind error\n")
closesocket(sockfd)
exit(1)
}
//listen,监听端口
if (listen(sockfd, BACKLOG) == -1)
{
//listen失败,退出程序
printf("listen error\n")
closesocket(sockfd)
exit(1)
}
printf("listen...")
//等待客户端连接
sin_size = sizeof(struct sockaddr_in)
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
printf("accept error\n")
closesocket(sockfd)
exit(1)
}
printf("\naccept!\n")
while(1) {
if((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1) continue
if(!strcmp(buf,"bye"))
{
//成功,关闭套接字
closesocket(sockfd)
closesocket(new_fd)
return 0
}
printf("%s %d",buf,strlen(buf))
sprintf(msg,"%d",strlen(buf))
if (send(new_fd,msg,MAXDATASIZE, 0) == -1)
{
printf("send ERRO")
closesocket(sockfd)
closesocket(new_fd)
return 0
}
}
}
你上面给出的代码其实就是 MSDN 里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的 TCP 通讯程序,可以在同一个局域网内的两台不同计算机之间聊天 :
这其实就是某本将网络通雀灶余讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:
========================
下面是公共代码:
========================
#ifndef __CINITSOCK__H__
#define __CINITSOCK__H__
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment( lib, "ws2_32.lib" )
#pragma comment( lib, "iphlpapi.lib" )
class CInitSock
{
public:
CInitSock( int nMinorVer = 2, int nMajorVer = 2 )
{
WSADATA wsData
WORD wVer = MAKEWORD( nMinorVer, nMajorVer )
if( 0 != 辩和WSAStartup( wVer, &wsData ) ) exit( 0 )
}
~CInitSock( )
{
WSACleanup( )
}
}
#endif
========================
下面是客户端的代码 :
=======================
#include "CInitSock.h"
#include <iostream>
using namespace std
CInitSock g_Sock
void main( )
{
SOCKET sockClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if( INVALID_SOCKET == sockClient )
return
sockaddr_in sockAddr
sockAddr.sin_family = AF_INET
sockAddr.sin_port = htons( 4567 )
sockAddr.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1" )
if( -1 == connect( sockClient, ( sockaddr* )&sockAddr, sizeof( sockAddr ) ) )
{
cout << "connect failed" << endl
return
}
while( true )
{
char szBuf[ MAX_PATH ]
ZeroMemory( szBuf, sizeof( szBuf ) )
cout << "You Say : "
cin >> szBuf
if( SOCKET_ERROR == send( sockClient, szBuf, MAX_PATH, 0 ) )
{
cout << "send failed" << endl
return
}
int nRecvLen = recv( sockClient, szBuf, MAX_PATH, 0 )
if( nRecvLen > 0 )
{
// szBuf[ nRecvLen ] = '\0'
cout << "Service Say : " << szBuf << endl <<顷滚 endl
}
else
{
cout << "recv failed" << endl
return
}
}
closesocket( sockClient )
}
======================
下面是服务端的代码:
======================
#include "CInitSock.h"
#include <iostream>
using namespace std
CInitSock g_Sock
void main( )
{
SOCKET sockClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if( INVALID_SOCKET == sockClient )
return
sockaddr_in sockAddr
sockAddr.sin_family = AF_INET
sockAddr.sin_port = htons( 4567 )
sockAddr.sin_addr.S_un.S_addr = INADDR_ANY
bind( sockClient, ( sockaddr* )&sockAddr, sizeof( sockAddr ) )
listen( sockClient, SOMAXCONN )
char szBuf[ MAX_PATH ]
sockaddr_in remoteAddr
int nLen = sizeof( sockaddr_in )
SOCKET sock = accept( sockClient, ( sockaddr* )&remoteAddr, &nLen )
while( true )
{
int nRecvLen = recv( sock, szBuf, MAX_PATH, 0 )
if( nRecvLen > 0 )
{
cout << "Client Say : " << szBuf << endl << endl
}
ZeroMemory( szBuf, sizeof( szBuf ) )
cout << "You Say : "
cin >> szBuf
if( SOCKET_ERROR == send( sock, szBuf, MAX_PATH, 0 ) )
{
cout << "send failed" << endl
}
}
closesocket( sock )
closesocket( sockClient )
}
希望能够帮到楼主 。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)