使用线程制作客户端服务器代码的正确形式是什么 [英] What is the right form for using threads to make a client server code

查看:63
本文介绍了使用线程制作客户端服务器代码的正确形式是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include<windows.h>
#include<winsock.h>
#include<winsock2.h>
#include<initguid.h>
#include<unistd.h>
#include<assert.h>
#include<malloc.h>
#include<thread>
#include<stdio.h>
//#include<pthread.h>
#include<boost/thread.hpp>
//#include<boost/thread/pthread/once.hpp>

void check_client(int argc, char** argv)
{
    int sockfd, conn_state;
    struct sockaddr_in serv_addr;
    struct hostent* serv;
    char* buff = (char*) malloc(sizeof(char*)*100);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if (argc != 3)
    {
        printf("Not enough User Input!\n\n");
        //exit(0);
        //break ;

    }
    if( sockfd < 0 )
    {
        printf("[!] Error creating socket file descriptor!\n\n");
       // exit(0);
       //break ;
    }

    serv = gethostbyname(argv[1]);

    if( serv == NULL)
    {
        printf("[!] Error getting hostname's address!\n\n");
        //exit(0);
        //break ;
    }
   /* return 0 ;*/

          }

    void server_Side (int argc, char** argv)

    {

     int sockfd , bind_state , acc_state, client_len;
     struct sockaddr_in server_addr, client_addr;
     //char buff =(char*)malloc(sizeof(char*)*100);
      char* buff = (char*) malloc(sizeof(char*)*100);
     sockfd = socket(AF_INET, SOCK_STREAM,0);
     if(sockfd<0)
     {
         printf("[!] error creating socket file! \n\n");
         //exit(0);
         //break ;
     }
    printf("waiting for connection");
    listen(sockfd,5);
    client_len= sizeof(client_addr);
    acc_state = accept(sockfd, (struct sockaddr*)&client_addr , &client_len);
    printf("acc_state : %d \n\n", acc_state);
    if(acc_state<0)
    {
        printf("[!] error accepting the coming connection from cliebt \n\n");
        //exit(0);
       // break ;
    }
     printf("communication established successfully with client\n");
     printf("[~] wiating any incoming buffer ... \n\n");
     read(acc_state , buff , 100+1);
     printf("[s] thanks for connecting", sizeof("[s] thanks again ")+1);

    close(sockfd);
   /* return 0;*/
   }


   using namespace std ;
//int main (int argc, char ** argv) {
int main () {
    using namespace boost ;
    char *cv , *cd , *argv ;
    int argc ;
    /*thread_1 = boost.thread ();
    thread_2 = boost.thread();
   boost.thread thread_1 = boost.thread (check_client , int argc , char** argv);
   boost.thread thread_2 = boost.thread (server_Side, int argc , char** argv);*/


     boost :: thread thread_1 = thread (server_Side, argc, &&argv);
    boost :: thread thread_2 = thread(check_client,  argc, &&argv);


   server_Side(4,&cd);
   check_client (4,&cv);

   thread_1.join();
   thread_2.join();


    return 0;
}






     /*std :: thread first (check_client,int argc,char** argv);
     std :: thread second (server_Side,int argc,char** argv);
     std :: THREAD_PRIORITY_IDLE (server_Side,int argc,char** argv);*/
    /* std :: THREAD_PRIORITY_IDLE ;
     first  = THREAD_PRIORITY_IDLE (check_client,int argc,char** argv);
     std :: THREAD_PRIORITY_IDLE; second = THREAD_PRIORITY_IDLE (server_Side,int argc,char** argv);*/
    /* std :: THREAD_ALL_ACCESS first (check_client,int argc,char** argv);
     std :: THREAD_ALL_ACCESS second (server_Side,int argc, char** argv);*/





我尝试了什么:



i尝试了所有内容,我在代码末尾写了它们但没有和我一起工作



What I have tried:

i tried everything and i wrote them at the end of the code but non worked with me

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



首先,在第一行执行断点并检查变量是否符合您的预期。然后逐行执行。

很奇怪,你的代码包含了你在转向线程之前应该掌握的很多基本错误。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

In first, put a breakpoint on very first line executed and check that variables meet your expectations. Then executed line by line.
Weird, your code contain very basic mistakes on things that you should have mastered long before turning to threads.


这篇关于使用线程制作客户端服务器代码的正确形式是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆