首页 | 资讯动态 | linux基础 | 系统管理 | 网络管理 | 编程开发 | linux数据库 | 服务器技术 | linux相关 | linux认证 | 嵌入式 | 下载中心 | 专题 | linux招聘 | 镜像站
OKLinux中文技术站
·设为首页
·加入收藏
·联系我们
系统管理: 中文环境 系统管理 桌面应用 内核技术 | Linux基础: 基础入门 安装配置 常用命令 经验技巧 软件应用 | Linux数据库: Mysql Postgre Oracle DB2 Sybase other
网络管理: 网络安全 网络应用 Linux服务器 环境配置 黑客安全 | 编程开发: PHP CC++ Python Perl Shell 嵌入式开发 java jsp | PHP技术: PHP基础 PHP技巧 PHP应用 PHP文摘
搜索中心 Linux招聘 Linux专题 Apache | Linux相关: 硬件相关 Linux解决方案 Linux认证 企业应用 其它Unix | 相关下载: 资料下载 参考手册 开发工具 服务器类 软路由 其它
 技术搜索:
会员中心 注册会员 高级搜索  
  → 当前位置:首页>编程开发>其他编程>正文

Linux下C网络编程(socket)

http://www.oklinux.cn  2008-08-21  linuxidc   会员收藏  游客收藏  【 】 
您查看的文章来源于http://www.oklinux.cn

经本人在NETTERM客户端测试,没发现什么问题.不过可能还有好多不合理的地方,希望各位大虾指正!

由于在我的博客上已经转载了几篇关于Linux下socket编程,所以此处只对头文件做简单介绍.

1.头文件介绍

errno.h

返回错误信息,用的是perro(),所以头文件有errno.h

netdb.h

定义struct hostent *gethostbyname(const char *hostname)要用的头文件.

#include
#include
#include

可能是网络编程用套节字必须的吧!

这是客户端程序,从服务器接受数据,别写入文件中.

QUOTE:
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define MAXDATASIZE 100

int main(int argc, char *argv[])
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
FILE *fp;
struct hostent *host;
struct sockaddr_in serv_addr;
if( argc < 2)
{
fprintf(stderr, "Please enter the server's hostname!\n");
exit(1);
}

if((host=gethostbyname(argv[1]))==NULL)
{
herror("gethostbyname出错!");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket创建出错!");
exit(1);
}

serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr = *(struct in_addr*)host->h_addr;
bzero(&(serv_addr.sin_zero),8);

if (connect(sockfd, (struct sockaddr *)&serv_addr,
sizeof(struct sockaddr)) == -1)
{
perror("connect出错!");
exit(1);
}

if ((fp = fopen("output_file", "w")) == NULL)
{
printf ("can't open file");
exit(0);
}

while (1)
{
memset(buf, 0, 100);
recvbytes=recv(sockfd, buf, 100, 0);

if (recvbytes == 0)
{
printf("数据已经接收完!");
close(fp);
close(sockfd);
exit(0);
}

printf("%s", buf);
fprintf(fp, "%s", buf);
}

close (fp);
close(sockfd);

return 0;
}


服务器从文件读入数据到缓冲区,再发送给客户端.

QUOTE:
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10
#define MAXDATASIZE 100

int main()
{
int sockfd,client_fd;
char buff[MAXDATASIZE];
FILE *fp;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
socklen_t sin_size = sizeof(struct sockaddr_in);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket创建出错!");
exit(1);
}

my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = inet_addr("192.168.1.211");
bzero(&(my_addr.sin_zero),8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
== -1)
{
perror("bind出错!");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1)
{
perror("listen出错!");
exit(1);
}
printf("server is running........\n");

if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr,
&sin_size)) == -1)
{
perror("accept出错");
exit(1);
}
else
{
printf("客户端已经连上\n");
}

printf("开始发送数据.......\n");

if ((fp = fopen("input_file", "r")) == NULL)
{
printf ("can't open file");
exit (0);
}

while (!feof(fp))
{
if (fgets(buff, 256, fp) == NULL)
{
close(client_fd);
close(sockfd);
printf("发送完毕,退出!\n");
exit(0);
}

if (send(client_fd, buff, 100, 0) == -1)
{
perror("send出错!");
close(client_fd);
exit(0);
}

printf ("send buff = %s", buff);
}

close(fp);
close(client_fd);
close(sockfd);

return 0;
}


这个是input_file中的信息

QUOTE:
name:xiaoxia sex:male age:90
name:xiaoxiao sex:male age:12
name:x sex:male age:15
name:xi sex:male age:32
name:xia sex:male age:18
name:xiao sex:male age:17
name:xiaox sex:male age:16
~

大家有兴趣的话可以拷贝程序试验,需要两个屏幕操作,结果所得到的文件output_file和input_file信息格式一样.


上一篇:Linux下用for循环卸载openoffice   下一篇:掌握IIS排错技巧让Web更好服务

收藏于收藏夹】 【评论】 【推荐】 【打印】 【关闭
相关文档
·Linux下用for循环卸载openoffice
·Ubuntu 下搭建 Rails 开发环境
·Ubuntu 8.04驱动开发环境
·Ubuntu升级内核手记
·关于Linux终端的显示
·Linux时间函数的应用
·在Ubuntu中用cron实现代码自动编译
·Linux触摸屏驱动解析
·移植FFmpeg到DaVinci开发环境上
·向Linux内核添加驱动
·Vim的一个bug
·Linux shell之grep
·嵌入式:关于Linux开发环境的建立
·Linux驱动与RTC驱动泛泛研究
·在Linux下用g 编译SDL程序的方法
·在Linux2.6内核下实现进程隐藏
发表评论
密码: 匿名评论
评论内容:

(不超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
 
  最新文档
·Linux下用for循环卸载openoffice
·Ubuntu 下搭建 Rails 开发环境
·Ubuntu 8.04驱动开发环境
·Ubuntu升级内核手记
·关于Linux终端的显示
·Linux时间函数的应用
·在Ubuntu中用cron实现代码自动编译
·Linux触摸屏驱动解析
·移植FFmpeg到DaVinci开发环境上
·向Linux内核添加驱动
·Vim的一个bug
·Linux shell之grep
  阅读排行
·开源空间 网络安全工具开发函数库Libne
·Linux下Qtopia Core 4.3(QT/E)交叉编译
·Linux编程时获取当前时间实例解析
·Linux socket编程实例:echo服务器程序
·Linux环境下OpenGL编程学习
·升级Redhat Linux 9.0内核有感
·GNU/Linux应用程序编程:用管道进行编
·Linux中断处理学习笔记
·Linux系统中限制用户进程CPU及内存占用
·解决Linux中Matlab中文乱码问题
·Linux下安装g77 fortran complier过程
·Linux环境下Wine的中文显示以及freetyp
·Linux多线程编程学习之线程同步
·SUSE Linux中安装Quartus 7.2过程笔记
·如何在Ubuntu 7.0上实现C/C++开发环境
网摘收藏: