// 写一段封指定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;
}
© 版权声明
本网站内容及资料来源于网络,并不代表本站观点和对其真实性负责,也不构成任何其他建议;部分内容是由网友自主投稿和发布、编辑整理上传,对此类内容本站仅提供交流平台,不为其版权负责;所提供的信息,只供参考之用,不保证信息的准确性、有效性、及时性和完整性;如果您发现网站上有侵犯您的知识产权的内容,请与我们取得联系,我们会及时修改或删除。文章版权归作者所有,原创作品未经允许请勿转载。投诉请联系:admin@chnhonker.com
THE END
暂无评论内容