新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于PXA270嵌入式系統的Socket通信設計

        基于PXA270嵌入式系統的Socket通信設計

        作者: 時間:2009-05-18 來源:網絡 收藏

        客戶端與服務器端進程的作用是非對稱的,因此編碼不同。同時,服務器進程一般是先于客戶請求請求而啟動的,只要運行,該服務進程一直存在,直到正常或強迫終止。服務器端主要程序如下:
        mysocket=socket(AF_INET,SOCK_STREAM,0);
        //建立新的套接字
        if(mysocket==-1)
        printf(error!!! failed to created mysocketn);
        return(0); //socket建立不成功,回初始位置
        mybindcheck=bind(mysocket,(struct sockaddr*)addr_local,sizeof(struct sockaddr));
        if(mybindcheck==-1)
        printf(error!!!failed to bind the IP and port with mysocketn);
        return(0);

        本文引用地址:http://www.104case.com/article/152489.htm

        圖4 服務器端程序簡單示意圖
        { printf(OK--you have successed bind your IP with port %dn,port);
        listencheck=listen(mysocket,howmany);
        if(listencheck==-1)
        printf(error!!! you have failed listen this port,program end heren);
        return(0); //調用監聽函數
        sin_size=sizeof(struct sockaddr_in);
        newsocket=accept(mysocket,(struct sockaddr*)addr_remote,sin_size);
        //調用接收函數
        if(newsocket==-1)
        printf(error!!!failed to got remote connect this server,program end heren);
        return(0); //建立新的socket失敗返回
        printf(OK-- now have created the newsocket to use this own connection,use this communicate with clint%sn,inet_ntoa(addr_remote.sin_addr),port);
        printf(OK-- server have successed got connect from clint IP = %s,port = %d,now connecting is running;n,inet_ntoa(addr_remote.sin_addr),port)
        pid_t pid;
        pid=fork(); //調用fork()建立子進程
        if(pid>0)
        printf(OK-- i am a father procces,child proccess will continue for you,it's ID= %d,now end newsock and use old socket to listen again................. n,pid);
        close(newsocket);
        printf(OK-- i am a child procces,i am responsible for this new communicate,blow i will do for connectn);
        printf(OK-- please enter your data which you want to send n);
        while(1)
        bzero(sendbuffer,long);
        scanf(%s,sendbuffer);
        sendcheck=send(newsocket,sendbuffer,strlen(sendbuffer),0); //發送數據
        if(sendcheck==-1)
        printf(error!!!failed to send to remoten);
        close(newsocket);
        else
        printf(OK--now you have send %d byte data to remote!!!pleases send again!!!!n,sendcheck);

        linux操作系統文章專題:linux操作系統詳解(linux不再難懂)

        tcp/ip相關文章:tcp/ip是什么




        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 平陆县| 通江县| 永定县| 武平县| 巩留县| 郸城县| 台南县| 建昌县| 宁远县| 黄大仙区| 滁州市| 桂东县| 社旗县| 云阳县| 公安县| 建宁县| 大同县| 陈巴尔虎旗| 三穗县| 济南市| 乌拉特后旗| 鄂伦春自治旗| 北票市| 仁布县| 德保县| 巴林右旗| 甘谷县| 宝丰县| 格尔木市| 将乐县| 九龙坡区| 德州市| 罗甸县| 滦南县| 慈溪市| 光山县| 洪江市| 腾冲县| 大姚县| 咸阳市| 万山特区|