- 分享
- 0
- 人气
- 0
- 主题
- 23
- 帖子
- 124
- UID
- 186218
- 积分
- 122
- 阅读权限
- 13
- 注册时间
- 2008-12-16
- 最后登录
- 2012-3-17
- 在线时间
- 358 小时
|
各位大大,我是用 DevC ++ 來編譯的。 主要是實現 Bakery Algorithm 但是編譯的時候 一直 出現
[Linker error] undefined reference to `ThreadFunc1@4'
ld returned 1 exit status
這兩個錯誤信息!不懂哪裡出現問題啊,請問怎樣解決 先謝謝大家
對了,如果你們要在 DEV ++ 編譯的話 你們要去 project option -> parameters -> linker 這個地方 add library object 過後在 DEV C 裡面 lib 的 folder 找 libwsock32.a 就可以了- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<winsock.h>
- #include<stdbool.h>
- #include<windows.h>
- #pragma comment(lib,"wsock32.lib")
- #define MAXLINE 1024
- #define MAXCLIENT 500
- DWORD WINAPI ThreadFunc1(PVOID pParam);
- CRITICAL_SECTION cs;
- int value=0;
- int number[MAXCLIENT];
- bool choosing[MAXCLIENT];
- int main()
- {
- SOCKET serv_sd,cli_sd;
- struct sockaddr_in serv,cli;
- int cli_len,n,a;
- WSADATA wsadata;
-
- for(a=0;a<MAXCLIENT;a++) //let all number be 0
- number[MAXCLIENT]=0;
-
- for(a=0;a<MAXCLIENT;a++) // let choosing array be 0
- choosing[a]=false;
-
- if(WSAStartup(0x101,(LPWSADATA)&wsadata)!=0) //check WSAStartup can use or not
- {
- fprintf(stderr,"echo_srv:can't use Winsock DLL\n");
- exit(1);
- }
-
- //open server socket
- serv_sd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET(use IPv4); SOCK_STREAM; 0(use default setting, means TCP)
-
- if(serv_sd==SOCKET_ERROR)
- {
- fprintf(stderr,"echo_srv:can't create the Server Socket\n");
- exit(1);
- }
-
- serv.sin_family=AF_INET;
- serv.sin_port=htons(9780);
- serv.sin_addr.s_addr=0;
-
- if(bind(serv_sd,(LPSOCKADDR)&serv,sizeof(serv))<0) // bind address to socket
- {
- fprintf(stderr,"echo_srv:can't bind local address\n");
- exit(1);
- }
-
- if(listen(serv_sd ,5)<0)
- {
- fprintf(stderr,"echo_srv:listen() error\n");
- exit(1);
- }
-
- printf("echo_srv:waiting for client\n");
-
- cli_len=sizeof(cli);
-
- while(1)
- {
- if((cli_sd=accept(serv_sd,(struct sockaddr *)&cli,&cli_len))==SOCKET_ERROR)
- {
- fprintf(stderr,"echo_srv:accept() error\n");
- closesocket(cli_sd);
- }
- else
- {
- printf("echo_srv: A client from [%d] come in",cli_sd);
- CreateThread(NULL,0,ThreadFunc1,&cli_sd,0,NULL);
- n=n+1;
- }
- }
-
- closesocket(serv_sd);
- closesocket(cli_sd);
- DeleteCriticalSection(&cs);
- WSACleanup();
-
- int max_number(int number[]) //subprogram of take number
- {
- int max=number[0],index;
-
- for(index=1;index<MAXCLIENT;index++)
- {
- if(max<number[index])
- max=number[index];
- }
- return max;
- }
-
- bool comp(int comp_number,int comp_socket,int now_number, int now_socket) //if more than one user take the same number then compare the ID
- {
- if(comp_number==now_number)
- {
- if(comp_socket<now_socket)
- return true;
- else
- return false;
- }
- else if(comp_number<now_number)
- return true;
- else
- return false;
- }
-
- DWORD WINAPI ThreadFunc1(PVOID pParam)
- {
- int n,temp,j,couter=0;
- char str[MAXLINE], result[MAXLINE], descript[MAXLINE]="is your answer \n\r",line[2]="\n";
-
-
-
- SOCKET temp_socket =*(SOCKET*)pParam;
- while(1)
- {
- if((n=recv(temp_socket,str,MAXLINE,0))==0)
- {
- fprintf(stderr,"echo_srv:[%d] connection closed\n",temp_socket);
- break;
- }
- else if(n==SOCKET_ERROR)
- {
- fprintf(stderr,"echo_srv:recv() error");
- break;
- }
- str[n]='\0';
-
- //temp_socket = thread_Id
- choosing[temp_socket]=true; // bakery start
- number[temp_socket] = max_number(number) +1 ;
- choosing[temp_socket]=false ;
- for(j=0;j<MAXCLIENT;j++)
- {
- while(choosing[j]);
- while(number[j]!=0 && comp(number[j],j,number[temp_socket],temp_socket));
- }
- // end
- value=value+ (int)str[0]-48; // let user input convert to int and assign to value
- itoa(value,result,10); //change integer to string
- strcat(result,descript); //let result and decript combine
-
- printf("echo_serv:%s add value from socket[%d]\n",str,temp_socket);
- printf("value is : %d\n",value);
-
- send(temp_socket,line,strlen(line),0);
-
- if(send(temp_socket,result,strlen(result),0)==SOCKET_ERROR)
- {
- fprintf(stderr,"echo_srv:connection closed");
- break;
- }
- number[temp_socket]=0;
-
- }
- }
- }
复制代码 |
|