这些代码我都是照着书上敲的,但是出现了问题
#include "WinSock2.h"
#include "stdio.h"
#include "stdlib.h"
#define DEFAULT_PORT 1234
void main()
{
int iPort = DEFAULT_PORT;
WSADATA wsaData;
SOCKET sListen, sAccept;
//客户端地址长度
int iLen;
//发送的数据长度
int iSend;
//要发送给客户端的信息
char buf[] = "I am a server.";
//服务器和客户端的IP地址
struct sockaddr_in ser, cli;
printf("--------------------\n");
printf("Server waiting\n");
printf("--------------------\n");
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\n");
return;
}
//创建服务器端套接口
sListen = socket(AF_INET, SOCK_STREAM, 0);
if (sListen == INVALID_SOCKET)
{
printf("socket() Failed:%d\n", WSAGetLastError());
return;
}
//以下建立服务器端地址
ser.sin_family = AF_INET;
//htons()函数把一个双字节的主机字节顺序的数转换为网络字节顺序的数
ser.sin_port = htons(iPort);
//htonl()函数把一个四字节主机字节顺序的数转换为网络字节顺序的数
//使用系统指定的IP地址INADDR_ANY
ser.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sListen, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR)
{
printf("bind() Failed:%d\n", WSAGetLastError());
return;
}
//进入监听状态
if (listen(sListen, 5) == SOCKET_ERROR)
{
printf("listen() Failed:%d\n", WSAGetLastError());
return;
}
//初始化客户端地址长度参数
iLen = sizeof(cli);
while (1)
{
sAccept = accept(sListen, (struct sockaddr*)&cli, &iLen);
if (sAccept == INVALID_SOCKET)
{
printf("accept() Failed:%d\n", WSAGetLastError());
break;
}
//输出客户端IP地址和端口号
printf("Accepted client IP:[%s],port:[%d]\n", inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
//给建立连接的客户端发送信息
iSend = send(sAccept, buf, sizeof(buf), 0);
if (iSend == SOCKET_ERROR)
{
printf("send(): Failed.:%d\n", WSAGetLastError());
break;
}
else if (iSend == 0)
break;
else
{
printf("send() byte:%d\n", iSend);
printf("--------------------\n");
}
closesocket(sAccept);
}
closesocket(sListen);
WSACleanup();
}
#include "WinSock2.h"
#include "stdio.h"
#include "stdlib.h"
#define DEFAULT_PORT 1234
void main()
{
int iPort = DEFAULT_PORT;
WSADATA wsaData;
SOCKET sListen, sAccept;
//客户端地址长度
int iLen;
//发送的数据长度
int iSend;
//要发送给客户端的信息
char buf[] = "I am a server.";
//服务器和客户端的IP地址
struct sockaddr_in ser, cli;
printf("--------------------\n");
printf("Server waiting\n");
printf("--------------------\n");
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\n");
return;
}
//创建服务器端套接口
sListen = socket(AF_INET, SOCK_STREAM, 0);
if (sListen == INVALID_SOCKET)
{
printf("socket() Failed:%d\n", WSAGetLastError());
return;
}
//以下建立服务器端地址
ser.sin_family = AF_INET;
//htons()函数把一个双字节的主机字节顺序的数转换为网络字节顺序的数
ser.sin_port = htons(iPort);
//htonl()函数把一个四字节主机字节顺序的数转换为网络字节顺序的数
//使用系统指定的IP地址INADDR_ANY
ser.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sListen, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR)
{
printf("bind() Failed:%d\n", WSAGetLastError());
return;
}
//进入监听状态
if (listen(sListen, 5) == SOCKET_ERROR)
{
printf("listen() Failed:%d\n", WSAGetLastError());
return;
}
//初始化客户端地址长度参数
iLen = sizeof(cli);
while (1)
{
sAccept = accept(sListen, (struct sockaddr*)&cli, &iLen);
if (sAccept == INVALID_SOCKET)
{
printf("accept() Failed:%d\n", WSAGetLastError());
break;
}
//输出客户端IP地址和端口号
printf("Accepted client IP:[%s],port:[%d]\n", inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
//给建立连接的客户端发送信息
iSend = send(sAccept, buf, sizeof(buf), 0);
if (iSend == SOCKET_ERROR)
{
printf("send(): Failed.:%d\n", WSAGetLastError());
break;
}
else if (iSend == 0)
break;
else
{
printf("send() byte:%d\n", iSend);
printf("--------------------\n");
}
closesocket(sAccept);
}
closesocket(sListen);
WSACleanup();
}