使用套接字的基于 TCP 协议网络通信的程序为什么老运行不出来呢?
服务器端#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
int main(int argc ,char * argv[] )//整数类型主函数(整数类型统计参数个数,字符类型*数组指针至字符[])
{
//判断是否输入了端口号
if(argc!=2)
{
printf("Usage: % s PortNumber\n",argv[0]);
exit(-1) ;
}
//把端口号化为整数
short port;
if((port = atoi (argv[1]))==0)
{
printf("端口号有误!");
exit(-1);
}
WSADATA wsa;
//初始化套接字DLL
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0)
{
printf("套接字初始化失败!");
exit(-1);
}
//创建套接字
SOCKET serverSocket;
if((serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
printf("创建套接字失败!");
exit(-1);
}
//构造完整的地址数据结构
struct sockaddr_in serverAddress;
memset(&serverAddress,0,sizeof(sockaddr_in));//将serverAddress变量清零
serverAddress.sin_family = AF_INET;//将协议族设置为TCP/IP协议
serverAddress.sin_addr .S_un.S_addr = htonl (INADDR_ANY);
serverAddress.sin_port = htons (port);//端口设置
//绑定
if(bind(serverSocket,(sockaddr * )&serverAddress,sizeof(serverAddress))==SOCKET_ERROR)
{
printf("套接字绑定到端口失败!端口: %d\n",port);
exit(-1);
}
//进入侦听状态
if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR)
{
printf("侦听失败!");
exit(-1);
}
printf("Server %d is listening......\n",port);
SOCKET clientSocket; //用来和客户端通信的套接字
struct sockaddr_in clientAddress; //用来和客户端通信的套接字地址
memset(&clientAddress,0,sizeof (clientAddress));
int addrlen = sizeof(clientAddress);
//接受连接
if((clientSocket=accept(serverSocket,(sockaddr* )&clientAddress,&addrlen))== INVALID_SOCKET)
{
printf("接受客户端连接失败!");
exit(-1);
}
printf("Accept connection from % s\n",inet_ntoa(clientAddress.sin_addr));
char buf[4096];
while(1)
{
//接收数据
int bytes;
if((bytes=recv(clientSocket,buf,sizeof(buf),0))==SOCKET_ERROR)
{
printf("接收数据失败!\n");
exit(-1);
}
buf[bytes]='\0';
printf("Message from %s: %s\n",inet_ntoa(clientAddress.sin_addr),buf);
if(send(clientSocket,buf,bytes,0)== SOCKET_ERROR)
{
printf("发送数据失败!");
exit(-1);
}
}
//清理套接字占用资源
WSACleanup();
return 0;
}
客户机端
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
int main(int argc,char * argv[])
{
//判断是否输入了IP地址和端口号
if(argc!=3)
{
printf("Usage: % s IPAddress PortNumber\n",argv[0]);
exit(-1);
}
//把字符串的IP地址转化为u_long
unsigned long ip;
if((ip = inet_addr(argv[1]))== INADDR_NONE)
{
printf("不合适的IP地址:%s",argv[1]);
exit(-1);
}
//把端口号化为整数
short port;
if((port = atoi(argv[2]))==0)
{
printf("端口号有误!");
exit(-1);
}
printf("Connecting to %s:%d......\n",inet_ntoa(*(in_addr*)&ip),port);
WSADATA wsa;
//初始化套接字DLL
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0)
{
printf("套接字初始化失败!");
exit(-1);
}
//创建套接字
SOCKET socket;
if((socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
printf("创建套接字失败!");
exit(-1);
}
struct sockaddr_in serverAddress;
memset(&serverAddress,0,sizeof(sockaddr_in));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.S_un.S_addr = ip;
serverAddress.sin_port = htons(port);
//建立和服务器的连接
if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))== SOCKET_ERROR)
{
printf("建立连接失!");
exit(-1);
}
char buf[4096];
while(1)
{
printf(">");
//从控制台读取一行数据
gets(buf);
//发送给服务器
if(send(sock,buf,strlen(buf),0)==SOCKET_ERROR)
{
printf("发送数据失败!");
exit(-1);
}
int bytes;
if((bytes = recv(socket,buf,sizeof(buf),0))== SOCKET_ERROR)
{
printf("接收数据失败!\n");
exit(-1);
}
buf[bytes]='\0';
//调用inet_ntoa函数将长整型地址转换为点分十进制字符串
printf("Message from %s: %s\n",inet_ntoa(serverAddress.sin_addr),buf);
}
//清理套接字占用资源
WSACleanup();
return 0;
}