首页
社区
课程
招聘
[求助]sockets编程遇到的问题
发表于: 2007-8-27 11:25 5749

[求助]sockets编程遇到的问题

2007-8-27 11:25
5749
小弟刚学习sockets编程,写了个小程序以实现类似telnet功能。
思路如下:
客户端接受命令并传给服务端,服务端使用system函数运行,
并利用cmd下的">"重定向符号将结果输入到"exeinf"文件中,
最后再将文件内容读入缓冲区发送到客户端以实现回现功能。
测试时发现如下问题:
1.客户端只能发送一次命令
2.发送“net user” 可以正确执行,但是发送“net localgroup admininstrators”
以及dir等命令不能正确执行(没回显)

读了几遍代码没找出原因,还望各位指教一下,谢谢;)

编译环境:xp & vc++6.0
代码如下:

//client.cpp
#include <stdio.h>
#include <winsock.h>
#include <windows.h>
#pragma comment(lib,"Ws2_32")

#define PORT 2007                                       
#define MAXDATASIZE 1024                                /* the max charectors recived */

        void GL_logo()
{
                        printf("study sockets.\n");
                       
}

int main(int argc, char *argv[])
{
        LoadLibrary("msvcrt.dll");

        int sockfd;
        int num;                    //control the loop
        struct sockaddr_in their_addr;       
        char Cmd[128];                //cmd buf which contens the command
        char buf[MAXDATASIZE];               
        char cinf;                    //read file buffer size of which is char
       
        system("cls");
        GL_logo();

        if (argc != 2)
        {                
                       
                printf(" Usage: %s ip\n",argv[0]);
                        exit(1);
        }

        WSADATA ws;
        WSAStartup(MAKEWORD(2,2),&ws);        //init Windows Socket Dll

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                printf("[+]Initialize socket error...\n");
                exit(1);
        }
        else
                printf("[+]Initialize sockets successful...\n");

        //connect to server
        their_addr.sin_family = AF_INET;                                        /* protol isINET*/
        their_addr.sin_port = htons(PORT);                                        /* connect to server ports*/
        their_addr.sin_addr.s_addr = inet_addr(argv[1]);        /* server IP*/

        if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
        {
                printf("[-]Connet error...\n");
                closesocket(sockfd);
                exit(1);
        }
        else
                printf("[-]IConnet successful...\n");

    //gain the command
        while(1)
{

                printf("[+]Please input the command:");
            num=0;
            while((cinf=getchar())!='\n')
          {
                Cmd[num]=cinf;
                num++;
          }
          Cmd[num]='\0';
          if(Cmd=="GL")
          {
                break;
          }
          printf("[-]The command you input is:\"%s\"\n",Cmd);
          sprintf(buf,"%s > exeinf",Cmd);

                //send command
          if (send(sockfd, buf, 14, 0) == -1)
        {
                printf("[+]Send error...");
                closesocket(sockfd);
                                exit(1);
        }
        printf("[+]Send ok...\n");

                //stop here

        //recive and print the message
        if ((num=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
        {
                //if error then exit
                printf("[-]Recived error...\n");
                closesocket(sockfd);
                exit(1);
        }
        else
                printf("[-]Recived successful...\n");

        buf[num] = '\0';
        printf("[^GL^]The result of \"%s\" is: \n%s\n",Cmd,buf);

}
        closesocket(sockfd);

        return 0;
}

_____________________________________________________________________________

//server.cpp
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib,"Ws2_32")
#define MYPORT 2007 /*defind the port*/
#define BACKLOG 14 /*the num of connect*/

int main()
{
        int sockfd, new_fd;                               
        struct sockaddr_in my_addr;               
        struct sockaddr_in their_addr;       
        int sin_size;

       
        char Cmd[128];                //cmd buf which contens the command
        char buf[1024];               
        char cinf;                    // buffer
        int num;                    
        FILE *file_cmd;
       

        WSADATA ws;
        WSAStartup(MAKEWORD(2,2),&ws);       

                if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
                {
                        //creat errror then exit
                        printf("socket error\n");
                        exit(1);
                }

        //bind本机的MYPORT端口
        my_addr.sin_family = AF_INET;                        /* protol is INET*/
        my_addr.sin_port = htons(MYPORT);                /* bind MYPORT        */
        my_addr.sin_addr.s_addr = INADDR_ANY;        /* MYIP        */
        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
                {
                        //bind error then exit
                        printf("bind error\n");
                        closesocket(sockfd);
                        exit(1);
                }
       
        //listen ports

        if (listen(sockfd, BACKLOG) == -1)
                {
                        //listen error then exit
                        printf("listen error\n");
                        closesocket(sockfd);
                        exit(1);
                }

        printf("listen...");

        //wating for connect to
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
                {
                        printf("accept error\n");
                        closesocket(sockfd);
                        exit(1);
                }
        printf("\naccept!\n");
while(1)
{
       
        //recive the command
        if ((num=recv(new_fd, Cmd, 128, 0)) != -1)
                {
                        printf("[+]Recived successful...\n");
                        //buf[num] = '\0';                                                
                        sprintf(buf,"%s > exeinf",Cmd);
                                                if( (file_cmd = fopen("exeinf", "w+" )) != NULL )
                                                        {
                                                                fclose( file_cmd );
                                                                system(buf);
                                                        }
                                                else
                                                        {
                                                                printf("built fiel errror");
                                                                closesocket(sockfd);
                                                                closesocket(new_fd);
                                                        }
                                                                //putout to the file
                           if( (file_cmd = fopen("exeinf", "r" )) != NULL )
                                        {
                                                cinf=fgetc(file_cmd);
                                                num=0;
                                                while(cinf!=EOF)
                                                        {
                                                                buf[num]=cinf;
                                                                num=num++;
                                                                cinf=fgetc(file_cmd);
                                                        }
                                                buf[num]='\0';
                                                printf("the result is:\n %s\n",buf);
                                        }
                           else
                                        {
                                                printf("can't read the file\n");
                                                fclose( file_cmd );
                                        }

                        //send the exec result
                                if (send(new_fd, buf, num, 0) == -1)
                                        {
                                                printf("send error");
                                                closesocket(sockfd);
                                                closesocket(new_fd);
                                                exit(1);
                                        }
                printf("send ok!\n");
                }

        //recive error then exit
        else
        {
                printf("[+]Recived error...\n");
                        exit(1);
        }
           
}

            closesocket(new_fd);
            closesocket(sockfd);
            

        return 0;
}

[注意]看雪招聘,专注安全领域的专业人才平台!

收藏
免费
支持
分享
最新回复 (8)
雪    币: 101
活跃值: (12)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
2
不愿看了, 一句话, 太乱了
   if(Cmd=="GL")   //你知道你想干嘛吗?
if (send(sockfd, buf, 14, 0) == -1)  //为什么要限制14?
sprintf(buf,"%s > exeinf",Cmd); //发送的时候要追加, 读的时候又追加?

另: >默认是stdout, 对于stderror的不重定向
2007-8-29 00:55
0
雪    币: 6075
活跃值: (2236)
能力值: (RANK:1060 )
在线值:
发帖
回帖
粉丝
3
从代码风格来看好专业啊…GNU?
从问题来看……唉
2007-8-29 02:14
0
雪    币: 226
活跃值: (15)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
4
我听见脚步声是预料的软皮鞋跟他推开门晚风好冷没有的预征
2007-8-29 03:45
0
雪    币: 171
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
if(Cmd=="GL")   //你知道你想干嘛吗?
-----------------------------------------
不好意思,贴的时候有删减,这个“GL”是用来判断是否退出的命令字符串:)

if (send(sockfd, buf, 14, 0) == -1)  //为什么要限制14?
------------------------------------------------------------
哦 这个14是有问题,有点偏小了

sprintf(buf,"%s > exeinf"....
----------------------------------------
这个是在构造cmd下的重定向命令
eg:  net user > exeinf
使net user 命令的回显输入到exeinf文件中,然后将exeinf内容读入buffer最后再发送给客户端实现回显

另: >默认是stdout, 对于stderror的不重定向
---------------------------------------------
呵呵 学习了 谢谢啊
2007-8-29 22:11
0
雪    币: 207
活跃值: (10)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
6
莪聽見腳步聲昰預料啲軟皮鞋哏彵推開闁晚闏恏冷莈洧啲預征
2007-8-29 22:15
0
雪    币: 171
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
见笑了
小弟刚接触socket,
这是处女作 哈哈
加之以前也没写程序的经验
献丑了
2007-8-29 22:17
0
雪    币: 101
活跃值: (12)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
8
if(Cmd=="GL")   字符串不是这么比较的。
那个发送和接收都追加, 会有2个>
这个不需要读文件, 可以创建cmd进程, 当然创建前要建立一个pipe. 然后把pipe的
handle设置成进程的标准输出和标准错误输出.
这样你的程序可以建立一个线程, 来readfile, 如果读到了直接发送.
2007-8-30 00:47
0
雪    币: 171
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
呵呵
学习了
好惭愧
努力中......
2007-8-30 13:54
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册