其实没什么特殊的,就是记录一下
开发板使用STM32F4,跑的uclinux系统,虚拟机ubuntu12.04
首先保证ARM_linux和虚拟机linux可以ping通,然后应该没什么问题
参考来源:http://blog.csdn.net/chencheng126/article/details/44260799
客户端:client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSIZE 4096
int main(int argc,char **argv)
{
if(argc!=2)
{
printf("usage: ./%s \n",argv[0]);
return 0;
}
int sfd;
int domain=AF_INET;
int type=SOCK_STREAM;
int protocol=0;
// char str_ip[15];
struct sockaddr_in cltaddr,seraddr;
char sendline[MAXSIZE],recvline[MAXSIZE];
if((sfd=socket(domain,type,protocol))==-1)
{
printf("socket error :%s\n",strerror(errno));
return 0;
}
memset(&seraddr,0,sizeof(seraddr));
/* if(inet_pton(domain,"192.168.1.112",&cltaddr.sin_addr)!=1)
{
printf("inet_pton1 error :%s\n",strerror(errno));
return 0;
}
*/
// cltaddr.sin_family=domain;
// cltaddr.sin_port=htons(7000);
/* if(bind(sfd,(struct sockaddr *)(&cltaddr),sizeof(cltaddr))==-1)
{
printf("bind error :%s\n",strerror(errno));
return 0;
}
*/
seraddr.sin_family=domain;
seraddr.sin_port=htons(8000);
if(inet_pton(domain,argv[1],&seraddr.sin_addr)!=1)
{
printf("inet_pton2 error :%s\n",strerror(errno));
return 0;
}
// printf("AF_INET=%d,%d\n",AF_INET,domain);
// printf("sockaddr->family:%d\n",seraddr.sin_family);
// printf("sockaddr->port:%d\n",seraddr.sin_port);
// printf("sockaddr->sin_addr:%d\n",seraddr.sin_addr.s_addr);
if(connect(sfd,(struct sockaddr *)(&seraddr),sizeof(seraddr))==-1)
{
printf("connect error :%s\n",strerror(errno));
return 0;
}
printf("send msg to server:");
scanf("%s",sendline);
if(send(sfd,sendline,strlen(sendline),0)<0)
{
printf("send msg error :%s\n",strerror(errno));
return 0;
}
int rec_len;
if((rec_len=recv(sfd,recvline,MAXSIZE,0))==-1)
{
printf("recv msg error\n");
return 0;
}
recvline[rec_len]='\0';
printf("the recv msg:%s\n",recvline);
close(sfd);
return 0;
}
服务器:server.c
#include
#include
#include
#include
#include
#include
#include
int initserver(int type,const struct sockaddr *addr,socklen_t len,int qlen)
{
printf("Server is running...\n");
int fd;
// int err=0;
if((fd=socket(addr->sa_family,type,0))<0)
{
printf("socket error\n");
return 0;
}
if(bind(fd,addr,len)<0)
{
printf("bing error\n");
close(fd);
return -1;
}
if(type==SOCK_STREAM||type==SOCK_SEQPACKET)
{
if(listen(fd,qlen)<0)
{
printf("listen error\n");
close(fd);
return -1;
}
}
return fd;
}
int main(void)
{
int socket_fd,connect_fd;
struct sockaddr_in seraddr;
char buff[4096];
seraddr.sin_family=AF_INET;
seraddr.sin_addr.s_addr=htonl(INADDR_ANY);
seraddr.sin_port=htons(8000);
if((socket_fd=initserver(SOCK_STREAM,(struct sockaddr *)&seraddr,sizeof(seraddr),10))==-1)
{
printf("initserver error\n");
return 0;
}
while(1)
{
if((connect_fd=accept(socket_fd,NULL,NULL))==-1)
{
printf("accept error:%s\n",strerror(errno));
continue;
}
int n=recv(connect_fd,buff,4096,0);
if(!fork())
{
if(send(connect_fd,"Hello,you are connected!\n",26,0)==-1)
printf("send error\n");
close(connect_fd);
return 0;
}
buff[n]='\0';
printf("recv msg from client:%s\n",buff);
close(connect_fd);
}
close(socket_fd);
return 0;
}