免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2851 | 回复: 7
打印 上一主题 下一主题

[C++] linux c++ 线程 从队列中取出端口号后出现乱码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-01 11:54 |只看该作者 |倒序浏览
发送端代码:
void* SendThread(void*)
{
        cout<<"create thread"<<endl;

        Send sendfile;

        for(;
        {
                if(sendqueue.empty())
                        continue;
pthread_mutex_lock(&sendMutex);
                if(!sendqueue.empty())
                {
                        sendJob newsendjob = sendqueue.pop();
                        pthread_mutex_unlock(&sendMutex);
                        sendfile.SendFile(newsendjob);
                        cout<<"client main one new sendjob has been send !"<<endl;
                }
        }
}

int Send::SendFile(sendJob newjob) {

cout<<"client send.cpp  " <<newjob.PRname<<endl;

        char Filepath[50];
        int localsock,connectret,Filefd;

        strcpy(Filepath,Client_FILE_PATH);
        strcat(Filepath,newjob.PRname);

std::cout<<"Filepath is :   "<<Filepath<<endl;

  struct sockaddr_in client;

  client.sin_family=AF_INET;
  client.sin_port=htons(DP_LISTEN_PORT);
  client.sin_addr.s_addr=inet_addr("127.0.0.1";

  localsock=socket(AF_INET,SOCK_STREAM,0);

  if(localsock<0)
  {
          perror("create socket failed!";
          exit(1);
  }

  cout<<"create socket successfull"<<endl;

  connectret=connect(localsock,(struct sockaddr*)&client,sizeof(client));

  if(connectret<0)
  {
          perror("connect failed!";
          exit(1);
  }

  cout<<"connect successfull"<<endl;

  Filefd=open(Filepath,O_RDONLY);

  int readline;
  char buf[1024];

  long t = lseek(Filefd,0,SEEK_SET);

  cout<<"return value of lseek is :  "<<t<<endl;

  write(localsock,newjob.PRname,sizeof(newjob.PRname));

  cout<<"1111   client  send.cpp   last :  " <<newjob.PRname<<endl;

  while((readline=read(Filefd,buf,sizeof(buf)))>0) {
          write(localsock,buf,readline);
  }

  close(localsock);

  cout<<"a File is transfered"<<endl;

  return 1;
}

接收端代码:

void* getRequestThread(void*)
{
        cout<<"start getRequestThread"<<endl;

        struct sockaddr_in server_addr;

  int listensock=socket(AF_INET,SOCK_STREAM,0);

  if (listensock<0)
  {
          perror("create listen socket failed!";
          exit(1);
  }
  cout<<"create socket successfull"<<endl;

  bzero(&server_addr,sizeof(server_addr));

  server_addr.sin_family=AF_INET;
  server_addr.sin_port=htons(DP_LISTEN_PORT);
  server_addr.sin_addr.s_addr=inet_addr("127.0.0.1";

  if(bind(listensock,(struct sockaddr*)&server_addr,sizeof(server_addr))<0)
  {
          perror("bind failed!";
          close(listensock);
          exit(1);
  }
  cout<<"bind successfull"<<endl;

  if(listen(listensock,5)<0)
  {
          perror("listen failed";
          exit(1);
  }

  cout<<"server is listen..."<<endl;

  int sockIn;
  struct sockaddr_in sock_in;
  socklen_t sin_size=sizeof(struct sockaddr_in);

  for(; {

          if((sockIn=accept(listensock,(struct sockaddr*)&sock_in,&sin_size))<0)
          {
                  perror("server accept failed!";
                  exit(1);
          }

          cout<<"get connection"<<endl;
          requestQueue.push_back(sockIn);

cout<<"server push back is :   "<<sockIn<<endl;

   }
}

void* ReceiveThread(void*)
{
        cout<<"receive thread"<<endl;

        Receive receive;

        while(1)
        {
                if(requestQueue.empty())
                        continue;
  pthread_mutex_lock(&receivemutex);
                if(!requestQueue.empty())
                {
                         char filename[10];
                        int localsockIn=requestQueue.front();
                        cout<<"got request"<<endl;
                        requestQueue.pop_front();
                        pthread_mutex_unlock(&receivemutex);
cout<<"servermain receivethread requestQueue.front is  :  "<<localsockIn<<"  and file name is  "<<filename<<endl;
                        receive.receiveMessage(localsockIn,filename);

                        cout<<"one file is received"<<endl;
                }
pthread_mutex_unlock(&receivemutex);
        }

}

receiveMessage部分代码:

int Receive::receiveMessage(int sockIn,char FILEName[10])
{
int readline,filefd;
char buf[1024];
char FileName[50];
char PathName[30];

cout<<"server sockIn is :   "<<sockIn<<endl;

        readline = read(sockIn,FileName,sizeof(FileName));
        cout<<"FileName is :  "<<FileName<<endl;

        char str[20];
        int i=0;
sprintf(str,"%d",i);
        strcpy(PathName,Server_FILE_PATH);
        strcat(PathName,str);
        strcat(PathName,FileName);

cout<<"server pathname is  "<<athName<<"  and FileName is  "<<FileName<<endl;

if((filefd=open(PathName,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
{
         perror("open file failed!");
         exit(1);
}

while((readline=read(sockIn,buf,sizeof(buf)))>0)
         write(filefd,buf,readline);
i++;
close(filefd);
close(sockIn);

return 1;
}


运行结果:
发送端:
create thread
client send.cpp  tcl_grammer.pdf
Filepath is :   /home/transferFile/client/tcl_grammer.pdf
create socket successfull
connect successfull
return value of lseek is :  0
1111   client  send.cpp   last :  tcl_grammer.pdf
end  ---------------
a File is transfered
client main one new sendjob has been send !
client send.cpp  tcl_grammer.pdf
Filepath is :   /home/transferFile/client/tcl_grammer.pdf
create socket successfull
connect successfull
return value of lseek is :  0
1111   client  send.cpp   last :  tcl_grammer.pdf

接收端:
start getRequestThread
create socket successfull
bind successfull
server is listen...
receive thread
get connection
server push back is :   4
got request
servermain receivethread requestQueue.front is  :  4  and file name is  
server sockIn is :   4
FileName is :  tcl_grammer.pdf
server pathname is  /home/transferFile/server/0tcl_grammer.pdf  and FileName is  tcl_grammer.pdf
one file is received
get connection
server push back is :   5
got request
servermain receivethread requestQueue.front is  :  -1219105904  and file name is  釻

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
2 [报告]
发表于 2009-04-01 12:08 |只看该作者
加锁试下

论坛徽章:
0
3 [报告]
发表于 2009-04-01 14:01 |只看该作者
蓝色部分是加锁的部分,加上之后还是不行阿?是不是加锁的地方错了?

论坛徽章:
0
4 [报告]
发表于 2009-04-01 14:08 |只看该作者
锁应该加在任何对其所保护内容有引用的地方,如果全部只读可以不加锁,但只要有一处有写操作就得全部加锁(原子写可除外,但考虑良好的编程习惯和可扩展性,建议还是用锁),LZ请你仔细看看你的代码,好多地方都没有用锁来保护

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
5 [报告]
发表于 2009-04-01 14:45 |只看该作者

回复 #3 zshuming616 的帖子

requestQueue.push_back(sockIn);
接收端的这个地方加下跟ReceiveThread相同的锁看看。

你的ReceiveThread写的不是很好,既然加锁不是很清晰,就索性把粒度弄大点吧。例如在你的ReceiveThread里
while进口加锁,出口解锁。

ps:接收端出问题,我说的是接收端,跟发送端没关系。(我看你把发送端也标出来了)

论坛徽章:
0
6 [报告]
发表于 2009-04-02 14:58 |只看该作者
我在接收端while处前和后加了加锁和解锁的代码,这下只能发一个文件了。我的初衷是发送端队列中保存的20个文件的目录,逐个取出之后,发给server,接收端收到后将文件改名,然后保存。现在只能发送一个文件了,请问该怎么改阿?谢谢!
SirFang 该用户已被删除
7 [报告]
发表于 2009-04-02 18:01 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2009-04-03 14:55 |只看该作者
This question has been resoved.Thank you everyone.

The problem of this question is I define the variable i in an error place.I should define it in the head of the file as an over situation variable.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP