| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 607 人关注过本帖
标题:[求助]这两段程序是在linux的c++下编的,有人能帮忙改成win下的吗?
只看楼主 加入收藏
vina
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-12-13
收藏
 问题点数:0 回复次数:0 
[求助]这两段程序是在linux的c++下编的,有人能帮忙改成win下的吗?

这是我的tcp/ip作业,其实我不是很懂,如果有高手肯给讲讲思路是最好的了~程序是在网上找的-_-程序的目的是能在服务器和客户端进行上传和下载。

这是客户端的程序

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#define PORT 3490 /* 客户将联接到这个端口 */

#define MAXDATASIZE 100 /* 一次能接收到的最大字节数 */

int main(int argc, char *argv[])

{

int sockfd, numbytes;

char buf[MAXDATASIZE];

struct hostent *he;

struct sockaddr_in their_addr; /* 联接者地址信息 */

if (argc != 2) {

fprintf(stderr,"usage: client hostname\\n");

exit(1);

}

if ((he=gethostbyname(argv[1])) == NULL) { /* 获取主机信息 */

herror("gethostbyname");

exit(1);

}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket");

exit(1);

}

their_addr.sin_family = AF_INET; /* 主机字节序 */

their_addr.sin_port = htons(PORT); /* short, 网络字节序 */

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */

if (connect(sockfd, (struct sockaddr *)&their_addr, \\

sizeof(struct sockaddr)) == -1) {

perror("connect");

exit(1);

}

if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {

perror("recv");

exit(1);

}

buf[numbytes] = \'\\0\';

printf("Received: %s",buf);

close(sockfd);

return 0;

}

这是服务器端的程序

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#define MYPORT 3490 /* 用户将联接到这个端口 */

#define BACKLOG 10 /* 保存未联接队列的数量 */

main()

{

int sockfd, new_fd; /* 在sock_fd上监听, 在new_fd上建立新的联接 */

struct sockaddr_in my_addr; /* 我的地址信息 */

struct sockaddr_in their_addr; /* 联接者的地址信息 */

int sin_size;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket");

exit(1);

}

my_addr.sin_family = AF_INET; /* 主机字节序 */

my_addr.sin_port = htons(MYPORT); /* short, 网络字节序*/

my_addr.sin_addr.s_addr = INADDR_ANY; /* 自动用主机的IP地址进行填充*/

bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \\

== -1) {

perror("bind");

exit(1);

}

if (listen(sockfd, BACKLOG) == -1) {

perror("listen");

exit(1);

}

while(1) { /* 主accept()循环 */

sin_size = sizeof(struct sockaddr_in);

if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \\

&sin_size)) == -1) {

perror("accept");

continue;

}

printf("server: got connection from %s\\n", \\

inet_ntoa(their_addr.sin_addr));

if (!fork()) { /*子进程*/

if (send(new_fd, "Hello, world!\\n", 14, 0) == -1)

perror("send");

close(new_fd);

exit(0);

}

close(new_fd); /* 父进程不要这样做 */

while(waitpid(-1,NULL,WNOHANG) > 0); /* 清除子进程 */

}

}
希望大家帮忙

搜索更多相关主题的帖子: 下编 win linux 
2005-12-13 18:21
快速回复:[求助]这两段程序是在linux的c++下编的,有人能帮忙改成win下的吗?
数据加载中...
 
   



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

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