| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 262 人关注过本帖
标题:C代码:远程缓冲区溢出
只看楼主 加入收藏
终结者C
Rank: 2
等 级:论坛游民
帖 子:34
专家分:23
注 册:2013-3-3
结帖率:100%
收藏
 问题点数:0 回复次数:0 
C代码:远程缓冲区溢出
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#define BUFFER_SIZE 1024
#define NAME_SIZE 2048
int handling(int c)
{
    char buffer[BUFFER_SIZE], name[NAME_SIZE];
    int bytes;
    strcpy(buffer, "My name is: ");
    bytes = send(c, buffer, strlen(buffer), 0);
    if (bytes == -1)
        return -1;
    bytes = recv(c, name, sizeof(name), 0);
    if (bytes == -1)
        return -1;
    name[bytes - 1] = ’\0’;
    sprintf(buffer, "Hello %s, nice to meet you!\r\n",name);
    bytes = send(c, buffer, strlen(buffer), 0);
    if (bytes == -1)
                        return -1;  
}
int main(int argc, char *argv[])
{
    int s, c, cli_size;
    struct sockaddr_in srv, cli;
    if (argc != 2)
    {
        fprintf(stderr, "usage: %s port\n", argv[0]);
        return 1;
    }
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s == -1)
    {
        perror("socket() failed");
        return 2;
    }
    srv.sin_addr.s_addr = INADDR_ANY;
    srv.sin_port = htons( (unsigned short int)
    atol(argv[1]));
    srv.sin_family = AF_INET;
    if (bind(s, &srv, sizeof(srv)) == -1)
    {
        perror("bind() failed");
        return 3;
    }
    if (listen(s, 3) == -1)
    {
        perror("listen() failed");
        return 4;
    }
    for(;;)
    {
        c = accept(s, &cli, &cli_size);
        if (c == -1)
        {
            perror("accept() failed");
                        return 5;
                }
                printf("client from %s", inet_ntoa(cli.sin_addr));
                if (handling(c) == -1)
                    fprintf(stderr, "%s: handling() failed", argv[0]);
                close(c);
         }
         return 0;
}
搜索更多相关主题的帖子: include return 缓冲区 
2013-03-05 07:37
快速回复:C代码:远程缓冲区溢出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027742 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved