主题:郁闷死了 怎么运行?
#include "winsock.h"
#include <stdio.h>
#define TRUE 1
/* 这个程序建立一个套接字,然后开始无限循环;每当它通过循环接收到一个连接,则打印出一个信息。当连接断开,或接收到终止信息,则此连接结束,程序再接收一个新的连接。命令行的格式是:streams */
void main( )
{
int sock, length;
struct sockaddr_in server;
struct sockaddr tcpaddr;
int msgsock;
char buf[1024];
int rval, len;
/* 建立套接字 */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
/* 使用任意端口命名套接字 */
server.sin_family = AF_INET;
server.sin_port = INADDR_ANY;
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("binding stream socket");
exit(1);
}
/* 找出指定的端口号并打印出来 */
length = sizeof(server);
if (getsockname(sock, (struct sockaddr *)&server, &length) < 0) {
perror("getting socket name");
exit(1);
}
printf("socket port #%d\n", ntohs(server.sin_port));
/* 开始接收连接 */
listen(sock, 5);
len = sizeof(struct sockaddr);
do {
msgsock = accept(sock, (struct sockaddr *)&tcpaddr, (int *)&len);
if (msgsock == -1)
perror("accept");
else do{
memset(buf, 0, sizeof(buf));
if ((rval = recv(msgsock, buf, 1024,0)) < 0)
perror("reading stream message");
if (rval == 0)
printf("ending connection \n");
else
printf("-->%s\n", buf);
}while (rval != 0);
closesocket(msgsock);
} while (TRUE);
/* 因为这个程序已经有了一个无限循环,所以套接字"sock"从来不显式关闭。然而,当进程被杀死或正常终止时,所有套接字都将自动地被关闭。*/
exit(0);
}
编译正常
但是构件exe时
ocket.obj : error LNK2001: unresolved external symbol _closesocket@4
socket.obj : error LNK2001: unresolved external symbol _recv@16
socket.obj : error LNK2001: unresolved external symbol _accept@12
socket.obj : error LNK2001: unresolved external symbol _listen@8
socket.obj : error LNK2001: unresolved external symbol _ntohs@4
socket.obj : error LNK2001: unresolved external symbol _getsockname@12
socket.obj : error LNK2001: unresolved external symbol _bind@12
socket.obj : error LNK2001: unresolved external symbol _socket@12
Debug/socket.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.
socket.exe - 9 error(s), 0 warning(s)
#include <stdio.h>
#define TRUE 1
/* 这个程序建立一个套接字,然后开始无限循环;每当它通过循环接收到一个连接,则打印出一个信息。当连接断开,或接收到终止信息,则此连接结束,程序再接收一个新的连接。命令行的格式是:streams */
void main( )
{
int sock, length;
struct sockaddr_in server;
struct sockaddr tcpaddr;
int msgsock;
char buf[1024];
int rval, len;
/* 建立套接字 */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
/* 使用任意端口命名套接字 */
server.sin_family = AF_INET;
server.sin_port = INADDR_ANY;
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("binding stream socket");
exit(1);
}
/* 找出指定的端口号并打印出来 */
length = sizeof(server);
if (getsockname(sock, (struct sockaddr *)&server, &length) < 0) {
perror("getting socket name");
exit(1);
}
printf("socket port #%d\n", ntohs(server.sin_port));
/* 开始接收连接 */
listen(sock, 5);
len = sizeof(struct sockaddr);
do {
msgsock = accept(sock, (struct sockaddr *)&tcpaddr, (int *)&len);
if (msgsock == -1)
perror("accept");
else do{
memset(buf, 0, sizeof(buf));
if ((rval = recv(msgsock, buf, 1024,0)) < 0)
perror("reading stream message");
if (rval == 0)
printf("ending connection \n");
else
printf("-->%s\n", buf);
}while (rval != 0);
closesocket(msgsock);
} while (TRUE);
/* 因为这个程序已经有了一个无限循环,所以套接字"sock"从来不显式关闭。然而,当进程被杀死或正常终止时,所有套接字都将自动地被关闭。*/
exit(0);
}
编译正常
但是构件exe时
ocket.obj : error LNK2001: unresolved external symbol _closesocket@4
socket.obj : error LNK2001: unresolved external symbol _recv@16
socket.obj : error LNK2001: unresolved external symbol _accept@12
socket.obj : error LNK2001: unresolved external symbol _listen@8
socket.obj : error LNK2001: unresolved external symbol _ntohs@4
socket.obj : error LNK2001: unresolved external symbol _getsockname@12
socket.obj : error LNK2001: unresolved external symbol _bind@12
socket.obj : error LNK2001: unresolved external symbol _socket@12
Debug/socket.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.
socket.exe - 9 error(s), 0 warning(s)