分类: 嵌入式
2010-08-10 11:10:33
完成一个TCP实验。
原理:
1 TCP/IP协议。
2 socket编程。
3 TCP流程。
TCP编程的服务器端一般步骤是:
1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt(); * 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();
4、开启监听,用函数listen();
5、接收客户端上来的连接,用函数accept();
6、收发数据,用函数send()和recv(),或者read()和write();
7、关闭网络连接;
8、关闭监听;
TCP编程的客户端一般步骤是:
1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt();* 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选
4、设置要连接的对方的IP地址和端口等属性;
5、连接服务器,用函数connect();
6、收发数据,用函数send()和recv(),或者read()和write();
7、关闭网络连接;
步骤:
1 在宿主机端编辑,编译,调试。
注意:由于主机端运行server,所以server.c要进行gcc编译。而client在开发板运行,所以进行交叉编译。
Server Makefile
EXEC = server
OBJS = server.o
SRC = server.c
CC = gcc
#LD = ld
CFLAGS += -O2 -Wall
LDFLAGS +=
all:$(EXEC)
$(EXEC):$(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
@rm -vf $(EXEC) *.o *~
Client Makefile
EXEC = client
OBJS = client.o
SRC = client.c
CC = arm-linux-gcc
#LD = arm-linux-ld
CFLAGS += -O2 -Wall
LDFLAGS +=
all:$(EXEC)
$(EXEC):$(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
@rm -vf $(EXEC) *.o *~
2 在主机端执行server程序,开发板终端执行client。结果如下:
由上图可以看出,服务器(主机)向客户端(开发板)发送信息,客户端成功接收,二者实现了通信功能。
源代码来自周立功:
Server.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5000
#define BACKLOG 10
#define LENGTH 512
int main()
{
int sockfd;
int nsockfd;
int num;
int sin_size;
char sdbuf[LENGTH];
struct sockaddr_in addr_local;
struct sockaddr_in addr_remote;
if((sockfd=socket(AF_INET,SOCK_STREAM ,0))==-1)
{
printf("ERROR:Failed to obtain Socket Descriptor.\n");
return(0);
}
else
{
printf("OK:Obtain Socket Desperitor successfully.\n");
}
addr_local.sin_family=AF_INET;
addr_local.sin_port=htons(PORT);
addr_local.sin_addr.s_addr=INADDR_ANY;
bzero(&(addr_local.sin_zero),8);
if(bind(sockfd,(struct sockaddr*)&addr_local,sizeof(struct sockaddr))==-1)
{
printf("ERROR:Failed to bind Port %d.\n",PORT);
return(0);
}else{
printf("OK:Bind the Port %d successfully.\n",PORT);
}
if(listen(sockfd,BACKLOG)==-1)
{
printf("ERROR:Failed to listen Port %d.\n",PORT);
return(0);
}else{
printf("OK:Listening the Port %d successfully.\n",PORT);
}
while(1)
{
sin_size=sizeof(struct sockaddr_in);\
if((nsockfd=accept(sockfd,(struct sockaddr*)&addr_remote,&sin_size))==-1){
printf("ERROR:Obtain new Socket Despcritor error.\n");
continue;
}else{
printf("OK:Server had got connect from %s.\n",inet_ntoa(addr_remote.sin_addr));
}
if(!fork())
{
printf("You can enter string and press 'exit' to end the connect.\n");
while(strcmp(sdbuf,"exit")!=0)
{
scanf("%s",sdbuf);
if((num =send(nsockfd,sdbuf,strlen(sdbuf),0))==-1)
{
printf("ERROR:Failed to sent string.\n");
close(nsockfd);
exit(1);
}
printf("OK:Sent %d bytes sucessful,please enter again.\n",num);
}
}
close(nsockfd);
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
Client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5000
#define LENGTH 256
int main(int argc, char *argv[])
{
int sockfd;
int num;
char revbuf[LENGTH];
struct sockaddr_in remote_addr;
if(argc!=2){
printf("Usage:client HOST IP (ex:./client 172.16.23.195).\n");
return(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
printf("ERROR:Failed to obtain Socket Descriptor!\n");
return(0);
}
remote_addr.sin_family=AF_INET;
remote_addr.sin_port=htons(PORT);
inet_pton(AF_INET,argv[1],&remote_addr.sin_addr);
bzero(&(remote_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr*)&remote_addr,sizeof(struct sockaddr))==-1){
printf("ERROR:Failed to connect to the host!\n");
return(0);
}else{
printf("OK:Have connected to the %s\n",argv[1]);
}
while(strcmp(revbuf,"exit")!=0){
bzero(revbuf,LENGTH);
num=recv(sockfd,revbuf,LENGTH,0);
switch(num)
{
case -1:
printf("ERROR:Receive string error!\n");
close(sockfd);
return(0);
case 0:
close(sockfd);
return(0);
default:
printf("OK:Received numbytes =%d\n",num);
break;
}
revbuf[num]='\0';
printf("OK:Received string is:%s\n",revbuf);
}
close(sockfd);
return(0);
}