写一段封指定ip的程序

// 写一段封指定ip的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{
    int sockfd;
    int n;
    struct sockaddr_in addr;
    struct sockaddr_in servaddr;
    char buf[1024];
    int i;
    char *ip;
    int optval;
    char *port;
    int flag;
    int fd;
    int ret;
    int count;
    int len;
    int j;
    int k;

    if (argc != 3)
    {
        printf("Usage: %s ip port\n", argv[0]);
        return -1;
    }
    ip = argv[1];
    port = argv[2];
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket");
        return -1;
    }
    optval = 1;
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("connect");
        return -1;
    }
    printf("connect success\n");
    flag = 1;
    while (flag)
        while (1)
        {
            printf("please input:");
            fgets(buf, 1024, stdin);
            buf[strlen(buf) - 1] = '\0';
            if (strcmp(buf, "quit") == 0)
            {
                flag = 0;
                break;
            }
            len = strlen(buf);
            for (i = 0; i < len; i++)
            {
                if (buf[i] == ' ')
                {
                    buf[i] = '\0';
                    for (j = i; j < len; j++)
                    {
                        buf[j] = buf[j + 1];
                    }
                    len--;
                    i--;
                }
            }
            count = 0;
            for (i = 0; i < len; i++)
            {
                if (buf[i] == '\0')
                {
                    count++;
                }
            }
            if (count == 0)
            {
                printf("please input again\n");
                continue;
            }
            for (i = 0; i < count; i++)
            {
                if (i == 0)
                {
                    if (send(sockfd, buf, strlen(buf), 0) < 0)
                    {
                        perror("send");
                        return -1;
                    }
                }
                else
                {
                    if (send(sockfd, " ", 1, 0) < 0)
                    {

                        perror("send");
                        return -1;
                    }
                    if (send(sockfd, buf, strlen(buf), 0) < 0)
                    {
                        perror("send");
                        return -1;
                    }
                }
            }
            printf("send success\n");
        }
    close(sockfd);
    return 0;
}

 

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容