socket 代理问题

antsmoving 2009-04-13 04:17:08
根据书上的代码写了个 TCP 的 client 代码 可以和server简单通信
代码如下

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define SERVERPORT 8888
#define MAXBUF 1024

int main(int argc, char* argv[])
{
int sockd;
int counter;
int fd;
struct sockaddr_in xferServer;
char buf[MAXBUF];
int returnStatus;

if (argc < 3)
{
fprintf(stderr, "Usage: %s <ip address> <filename> [dest filename]\n",
argv[0]);
exit(1);
}
/* create a socket */
sockd = socket(AF_INET, SOCK_STREAM, 0);
if (sockd == -1)
{
fprintf(stderr, "Could not create socket!\n");
exit(1);
}
/* set up the server information */
xferServer.sin_family = AF_INET;
xferServer.sin_addr.s_addr = inet_addr(argv[1]);
xferServer.sin_port = htons(SERVERPORT);
/* connect to the server */
returnStatus = connect(sockd,
(struct sockaddr*)&xferServer,
sizeof(xferServer));
if (returnStatus == -1)
{
fprintf(stderr, "Could not connect to server!\n");
exit(1);
}

/* send the name of the file we want to the server */
returnStatus = write(sockd, argv[2], strlen(argv[2])+1);
if (returnStatus == -1)
{
fprintf(stderr, "Could not send filename to server!\n");
exit(1);
}

/* call shutdown to set our socket to read-only */
shutdown(sockd, SHUT_WR);

/* open up a handle to our destination file to receive the contents */
/* from the server */
fd = open(argv[3], O_WRONLY | O_CREAT | O_APPEND);
if (fd == -1)
{
fprintf(stderr, "Could not open destination file, using stdout.\n");
fd = 1;
}

/* read the file from the socket as long as there is data */
int i = 0;
while ((counter = read(sockd, buf, MAXBUF)) > 0)
{
/* send the contents to stdout */
fprintf(stdout, "write for the %d time \n", ++i);
write(fd, buf, counter);
}
if (counter == -1)
{
fprintf(stderr, "Could not read file from socket!\n");
exit(1);
}
close(sockd);
return 0;
}

客户端给出服务器地址和端口 以及服务器段的一个文件名 发出后 服务器返回文件内容


现在我想添加代理功能, 改成 客户端通过代理 发送网页请求 读取指定网页的页面内容


代码应该如何修改, 查了网上的说法, 要添加 http协议内容, 请具体指点 谢谢
...全文
103 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
antsmoving 2009-04-15
  • 打赏
  • 举报
回复
试着看了下 太大了 没看下去
刚学unix下编程 估计看了也没什么用处

我问了同事, 同事说是把连接联到本地代理 然后把数据请求通过代理发送 我现在这么做了 只是把连接联到本地代理
但请求的内容不会, 得到的网页文件是空白的


哪位能直接指点指点 谢谢
morris88 2009-04-14
  • 打赏
  • 举报
回复
哎呀,看看 wget 是怎么实现的,貌似它支持代理吧...
once_and_again 2009-04-14
  • 打赏
  • 举报
回复
.bashrc

中配置 http_proxy
antsmoving 2009-04-13
  • 打赏
  • 举报
回复
打不开阿
  • 打赏
  • 举报
回复
参考下
https://forum.csdn.net/PointForum/Old/TopicRedirect.ashx?id=5725370

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧