|
我编写了一个程序,当中用了socket套接口,代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SIZE 40
#define SERVER_PORT 8100
int main(int argc,char *argv[]){
int sockfd,addrlen;
struct sockaddr_in server;
void udpconn(int sockfd,struct sockaddr_in *addr,int len);
sockfd=sock(AF_INET,SOCK_DGRAM,0);
if(sockfd<0){
perror("sock error:");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(SERVER_PORT);
inet_aton(argv[1],server.sin_addr);
addrlen=sizeof(server);
udpconn(sockfd,&server,addrlen);
exit(0);
}
void udpconn(int sockfd,struct sockaddr_in *addr,int len){
char msg[SIZE];
printf("Enter the word about information you want to know: ");
fgets(msg,40,stdin);
if(msg!=NULL){
sendto(sockfd,msg,strlen(msg),0,(struct sockaddr *)addr,len);
}
}
当我编译时:
gcc server.c
总会出现类似这样的错误:
/tmp/cc2wpt1M.o(.text+0x1a): In function `main':
: undefined reference to `sock'
collect2: ld returned 1 exit status
C的语法应该没错啊,是不是用gcc编译socket编写的程序时要加什么选项???
我写的这个程序很急要交,在此谢谢各位了,谢谢谢谢,望大家指教 |
|