Linux下C网络编程(socket)

经本人在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信息格式一样.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzyjzx.html