首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

网络编程中的线程有关问题

2012-08-02 
网络编程中的线程问题(大神们,求解决方案)为实现自由地收发信息,模拟聊天,创建线程。把文件标示符定义到全

网络编程中的线程问题
(大神们,求解决方案)
为实现自由地收发信息,模拟聊天,创建线程。把文件标示符定义到全局变量
但系统对全局变量的数据默认为0。那线程就应该不停循环。
我的程序执行结果:客服端可以自由接收和发送信息给服务端。但服务端只能
发信息,却接收不到信息。
服务端代码:
[code=C/C++][/code]
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ctype.h>
#include <pthread.h>
#define portnumber 3333

int sockfd,new_fd;
void *myThread( )
{
  int n=0;
  char buffer1[1024];
  while(1)
  {  
  if((n=read(new_fd,buffer1,strlen(buffer1)))==-1)
  {
  printf("Read error:%s\n",strerror(errno));
exit(1);
  }
  buffer1[n]='\0';
  printf("\nServer recive :%s\n",buffer1);
  }
}
int main()
{
   
  account *p;
  char buffer[1024];
  struct sockaddr_in server_addr;
  struct sockaddr_in client_addr;
  int size;
  int nbytes;
  pthread_t id;
  int ret=0;
  if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  {
  printf("socket error:%s\n",strerror(errno));
exit(1);
  }
  bzero(&server_addr,sizeof(struct sockaddr_in));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  server_addr.sin_port=htons(portnumber);
  int n = 1;
  setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n));
  if(bind(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr_in))==-1)
  {
  printf("Bind error:%s\n",strerror(errno));
exit(1);
  }
  if(listen(sockfd,5)==-1)
  {
  printf("Listen error:%s\n",strerror(errno));
exit(1);
  }

  size = sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&size))==-1)
{
printf("Accpet error:%s\n",strerror(errno));
exit(1);
}
ret=pthread_create(&id,NULL,(void*)myThread,NULL);
if(ret)
{
printf("pthread create error:%s\n",strerror(errno));
exit(1);
}
  while(1)
  {
printf("please input :");
fgets(buffer,1024,stdin);
printf("%s\n",buffer);
if(write(new_fd,buffer,strlen(buffer))==-1)
{
printf("Write error :%s\n",strerror(errno));
exit(1);
}
 
   

  }
  close(sockfd);
  exit(0);
}
客服端代码:
[code=C/C++][/code]

#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pthread.h>
#define portnumber 3333

int sockfd;
void *myThread( )
{
  char buffer1[1024];
  int n=0;
  while(1)
  {
  if((n=read(sockfd,buffer1,1024))==-1)
  {
  printf("Clinet read error:%s\n",strerror(errno));
exit(1);
  }
  buffer1[n]='\0';
  printf("\nClinet recive :%s\n",buffer1);
  }
}
int main(int argc,char *argv[])
{
  pthread_t id;
  int ret=0;
  char buffer[1024];
  struct sockaddr_in server_addr;


  struct hostent *host;
  account *p;
  if(argc!=2)
  {
  printf("Usage:%s hostname!\n");
exit(1);
  }
  if((host = gethostbyname(argv[1]))==NULL)
  {
  printf("Gethostname error!\n");
exit(1);
  }
  if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1)
  {
  printf("Socked Error:%s\n",strerror(errno));
exit(1);
  }
  bzero(&server_addr,sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(portnumber);
  server_addr.sin_addr=*((struct in_addr*)host->h_addr);
  if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
  {
  printf("Connect Error:%s\n",strerror(errno));
exit(1);
  }
  ret = pthread_create(&id,NULL,(void*)myThread,NULL);
  if(ret)
  {
  printf("Create pthread error:%s\n",strerror(errno));
exit(1);
  }
  while(1)
  {
  printf("please input:");
  fgets(buffer,1024,stdin);
  printf("%s\n",buffer);
  if( write(sockfd,buffer,strlen(buffer))==-1)
{
printf("Write error:%s\n",strerror(errno));
exit(1);
}
  }
   

  close(sockfd);
  exit(0);
}


[解决办法]

C/C++ code
  while(1)   {      if((n=read(new_fd,buffer1,[color=#FF0000]sizeof[/color](buffer1)))==-1)   //别用strlen   {   printf("Read error:%s\n",strerror(errno));
[解决办法]
特供调试多线程程序用:
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef WIN32    #include <windows.h>    #include <io.h>#else    #include <unistd.h>    #include <sys/time.h>    #include <pthread.h>    #define  CRITICAL_SECTION   pthread_mutex_t    #define  _vsnprintf         vsnprintf#endif//Log{#define MAXLOGSIZE 20000000#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))#include <time.h>#include <sys/timeb.h>#include <stdarg.h>char logfilename1[]="MyLog1.log";char logfilename2[]="MyLog2.log";char logstr[16000];char datestr[16];char timestr[16];char mss[4];CRITICAL_SECTION cs_log;FILE *flog;#ifdef WIN32void Lock(CRITICAL_SECTION *l) {    EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) {    LeaveCriticalSection(l);}#elsevoid Lock(CRITICAL_SECTION *l) {    pthread_mutex_lock(l);}void Unlock(CRITICAL_SECTION *l) {    pthread_mutex_unlock(l);}#endifvoid LogV(const char *pszFmt,va_list argp) {    struct tm *now;    struct timeb tb;    if (NULL==pszFmt||0==pszFmt[0]) return;    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;    ftime(&tb);    now=localtime(&tb.time);    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );    sprintf(mss,"%03d",tb.millitm);    printf("%s %s.%s %s",datestr,timestr,mss,logstr);    flog=fopen(logfilename1,"a");    if (NULL!=flog) {        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);        if (ftell(flog)>MAXLOGSIZE) {            fclose(flog);            if (rename(logfilename1,logfilename2)) {                remove(logfilename2);                rename(logfilename1,logfilename2);            }            flog=fopen(logfilename1,"a");            if (NULL==flog) return;        }        fclose(flog);    }}void Log(const char *pszFmt,...) {    va_list argp;    Lock(&cs_log);    va_start(argp,pszFmt);    LogV(pszFmt,argp);    va_end(argp);    Unlock(&cs_log);}//Log}int main(int argc,char * argv[]) {    int i;#ifdef WIN32    InitializeCriticalSection(&cs_log);#else    pthread_mutex_init(&cs_log,NULL);#endif    for (i=0;i<10000;i++) {        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);    }#ifdef WIN32    DeleteCriticalSection(&cs_log);#else    pthread_mutex_destroy(&cs_log);#endif    return 0;} 

热点排行