??λ???socket???????????
???????????? ???????[ 2013/2/18 10:27:45 ] ????????
???????????????????????????????????????????????????????????????????????????????????Щ??????????б?????д??????????????????????2???????????Linux?????close socket?????????????????????????е?????Щ???????socket?????????? read??recvfrom ????????????????????????????????????????????
?????????????????UDP??????????????socket??????????????????????????????????????????????????????shell?????????λ????????????????????????3????????socket????????????????????????????????
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
#include <pthread.h>
#define SERVER_PORT 8888
#define BUFFER_LEN 256
int g_Exit = 0;
void *service( void* arg )
{
char buff[BUFFER_LEN];
struct sockaddr clientAddr;
int socklen = sizeof(clientAddr);
int recvbytes;
int socketfd = *((int *)arg);
printf("OK?? Enter Service!
");
while(!g_Exit)
{
recvbytes = recvfrom(socketfd??buff??BUFFER_LEN??0??&clientAddr??&socklen);
sendto(socketfd??buff??recvbytes??0??&clientAddr??socklen);
}
printf("OK?? Service Thread Exit!
");
pthread_exit(NULL);;
}
int main( int argc??char * argv[] )
{
int fd;
void *status;
struct sockaddr_in serverAddr;
pthread_t thr;
pthread_attr_t attr;
fd = socket(AF_INET??SOCK_DGRAM??0);
memset(&serverAddr??0??sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(SERVER_PORT);
bind(fd??(struct sockaddr *)&serverAddr??sizeof(serverAddr));
// create service thread
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr?? PTHREAD_CREATE_JOINABLE);
if( pthread_create(&thr??&attr??service??(void *)&fd ) )
{
printf("pthread_create fail!
");
return -1;
}
// Free attribute
pthread_attr_destroy(&attr);
// wait user control exit
getchar();
getchar();
g_Exit = 1;
printf("OK?? Waiting For Thread Exit...!
");
close(fd);
// wait for thread exit
pthread_join(thr?? &status);
printf("OK?? Exit Main Process !
");
return 0;
}
??????????????????????λ?????????????£?
???????????????д????????????????????????????????????????????????????????????????????close socket???????????? recvfrom ?????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11