#include <stdio.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int welcomeSocket, newSocket;
char buffer[1024];
char str[1024];
int len,k,i;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
welcomeSocket= socket(PF_INET,SOCK_STREAM,0);
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(17100);
serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
memset(serverAddr.sin_zero,'\0',sizeof serverAddr.sin_zero);
bind (welcomeSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if(listen(welcomeSocket,5)==0)
printf("Listening!\n");
else
printf("Error\n");
addr_size= sizeof serverStorage;
newSocket=accept(welcomeSocket,(struct
sockaddr*)&serverStorage,&addr_size);
strcpy(buffer,"Enter string you want reversed or type exit: \n");
send(newSocket,buffer,strlen(buffer),0);
do
{
memset(buffer,0,strlen(buffer));
recv(newSocket,buffer,1024,0);
if(strcmp(buffer,"exit")==0)
break;
printf("%s\n",buffer);
len=strlen(buffer);
k=len-1;
for(i=0;i<len;i++)
{
str[i]=buffer[k];
k--;
}
send(newSocket,str,strlen(str),0);
}while(strcmp(buffer,"exit")!=0);
return 0;
}
编译此代码时出现以下错误:
gcc jason.c
未定义的第一个引用的
文件中的符号
bind /tmp/cc9aqprX.o
recv /tmp/cc9aqprX.o
发送/tmp/cc9aqprX.o
接受/tmp/cc9aqprX.o
听/tmp/cc9aqprX.o
socket /tmp/cc9aqprX.o
inet_addr /tmp/cc9aqprX.o
ld:致命:符号引用错误。没有输出写入a.out
collect2:ld返回1退出状态
请帮助解决此问题?