当前位置: 技术问答>linux和unix
多线程端口调用出了问题,帮忙看看为什么
来源: 互联网 发布时间:2015-11-12
本文导语: 我写了程序如下: #include #include #include #define time_wait 10 typedef struct _p_data { int socket; char message[100]; }pthread_data; int i=0; int nthreads=10; void *t_process(void *arg) { int connsock; ...
我写了程序如下:
#include
#include
#include
#define time_wait 10
typedef struct _p_data
{
int socket;
char message[100];
}pthread_data;
int i=0;
int nthreads=10;
void *t_process(void *arg)
{
int connsock;
char buff[]={"Hello! Welcome to here!"};
connsock=(*arg).socket;
send(connsock,buff,sizeof(buff), 0);
sleep(time_wait);
shutdown(connsock,2);
close(connsock);
i--;
//return NULL;
}
int main(int argc,char *argv[])
{
pthread_t *threadptr;
int listensock,connsock;
pthread_data* arg;
struct sockaddr_in serveraddr;
//const char buff[] = "Hello! Welcome to here!";
listensock = socket(AF_INET,SOCK_STREAM,0);
bzero(&serveraddr,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(5000);
bind(listensock,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
listen(listensock,1024);
arg=(pthread_data*)malloc(sizeof(pthread_data));
memset(arg, 0, sizeof(pthread_data));
threadptr=(pthread_t*)malloc(sizeof(pthread_t)*nthreads);
while(1)
{
connsock = accept(listensock,(struct sockaddr *)NULL, NULL);
if (connsock socket;
#include
#include
#include
#define time_wait 10
typedef struct _p_data
{
int socket;
char message[100];
}pthread_data;
int i=0;
int nthreads=10;
void *t_process(void *arg)
{
int connsock;
char buff[]={"Hello! Welcome to here!"};
connsock=(*arg).socket;
send(connsock,buff,sizeof(buff), 0);
sleep(time_wait);
shutdown(connsock,2);
close(connsock);
i--;
//return NULL;
}
int main(int argc,char *argv[])
{
pthread_t *threadptr;
int listensock,connsock;
pthread_data* arg;
struct sockaddr_in serveraddr;
//const char buff[] = "Hello! Welcome to here!";
listensock = socket(AF_INET,SOCK_STREAM,0);
bzero(&serveraddr,sizeof(struct sockaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(5000);
bind(listensock,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
listen(listensock,1024);
arg=(pthread_data*)malloc(sizeof(pthread_data));
memset(arg, 0, sizeof(pthread_data));
threadptr=(pthread_t*)malloc(sizeof(pthread_t)*nthreads);
while(1)
{
connsock = accept(listensock,(struct sockaddr *)NULL, NULL);
if (connsock socket;
|
connsock=(*arg).socket;
arg是void*类型,不能用*arg取值。
arg是void*类型,不能用*arg取值。