armlinux下的网路传输(tcp/ip) 分类: arm-linux-Ubuntu 2013-08-12 15:38 416人阅读 评论(0) 收藏
/*ex2serv.c*/
#include <time.h>
#include<stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define LISTENQ 5
#define MAXLINE 512
//char data2[10] = {0,1,2,3,4,5,6,7,8,9};
int listenfd, connfd;
char buff[MAXLINE];
void net_init()
{
int n=0;
FILE *fp;
//int x,y;
socklen_t len;
int i=0,k=0;
struct sockaddr_in servaddr, cliaddr;
char recvbuff[256];
//time_t ticks;
listenfd=socket(AF_INET, SOCK_STREAM,0);
if(listenfd<0)
{
printf("Socket created failed.\n");
return ;
}
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(6000);
//servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_addr.s_addr=inet_addr("192.168.253.99");
if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))<0)
{
printf("bind failed.\n");
return ;
}
printf("listening....\n");
listen(listenfd, LISTENQ);
//while(1)
{
len=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr, &len);
//printf("connect from %s, port %d \n",inet_ntoa(cliaddr.sin_addr.s_addr),ntohs(cliaddr.sin_port));
printf("listened OK\n");
}
}
void net_trans(unsigned char *buf,int len)
{
int tmp1=0,i=0;
//if(len<2000)
write(connfd,buf,len);
return;
//else
{
tmp1 = len/2000;
for(i=0;i<tmp1;i++)
write(connfd,buf+2000*i,2000);
tmp1 = len%2000;
if(tmp1!=0)
write(connfd,buf+2000*i,tmp1);
}
}
void net_close()
{
strcpy(buff,"over!\n");write(connfd,buff,strlen(buff));
close(connfd);
}
//*/
版权声明:本文为博主原创文章,未经博主允许不得转载。