UDP编程:
- 使用socket创建描述符
- 绑定bind本地ip地址端口
- 使用recvfrom接收数据
- 使用sendto发送数据
- 关闭描述符close(fd)
#include "stdio.h"
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <linux/net.h>
#include <arpa/inet.h>
#include "string.h"
char buffer[256];
int main(int argc,char*argv[])
{
struct sockaddr_in socket_addr;
struct sockaddr_in rx_addr;
printf("%s,%d test example \n",__FILE__,__LINE__);
int socket_fd = socket(AF_INET, SOCK_DGRAM,0);
socket_addr.sin_family = AF_INET;
socket_addr.sin_addr.s_addr = inet_addr("192.168.146.129");
socket_addr.sin_port = htons(6451);
int ret = bind(socket_fd,(__CONST_SOCKADDR_ARG)&socket_addr,sizeof(socket_addr));
printf("%s,%d bind ret %d\n",__FILE__,__LINE__,ret);
printf("%s,%d ip addr %x\n",__FILE__,__LINE__,socket_addr.sin_addr.s_addr);
memcpy(&rx_addr,&socket_addr,sizeof(rx_addr));
socket_addr.sin_family = AF_INET;
socket_addr.sin_addr.s_addr = inet_addr("192.168.9.128");
socket_addr.sin_port = htons(6001);
int len = 0;
ret = sendto(socket_fd,buffer,10,0,(__CONST_SOCKADDR_ARG)&socket_addr,sizeof(socket_addr));
memset(&rx_addr,0,sizeof(rx_addr));
while(1){
ret=recvfrom(socket_fd,buffer,256,0,(__SOCKADDR_ARG)&rx_addr,&len);
for(int i=0;i<256;i++){
buffer[i] = buffer[i]+1;
}
printf("%s,%d recvfrom ret %d\n",__FILE__,__LINE__,ret);
ret = sendto(socket_fd,buffer,ret,0,(__CONST_SOCKADDR_ARG)&socket_addr,sizeof(socket_addr));
printf("%s,%d sendto ret %d\n",__FILE__,__LINE__,ret);
printf("%d,%x\n",rx_addr.sin_port,rx_addr.sin_addr);
// sleep(1);
}
close(socket_fd);
return 0;
}
TCP client编程:
- 使用套接字socket创建数据流描述符
- 绑定本地地址bind端口
- 连接到客户端connect
- read接收数据
- write发送数据
- close关闭描述符
#include "stdio.h"
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <linux/net.h>
#include <arpa/inet.h>
#include "string.h"
#define CLEAR(type) memset(&type,0,sizeof(type))
int main(int argc,char*argv[])
{
printf("%s,%d test example \n",__FILE__,__LINE__);
char buffer[256];
int fd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr;
CLEAR(addr);
addr.sin_addr.s_addr = inet_addr("192.168.126.129");
addr.sin_family = AF_INET;
addr.sin_port = htons(4567);
int ret = bind(fd,(__CONST_SOCKADDR_ARG)&addr,sizeof(addr));
CLEAR(addr);
addr.sin_addr.s_addr = inet_addr("192.168.11.66");
addr.sin_family = AF_INET;
addr.sin_port = htons(6000);
ret = connect(fd,(__CONST_SOCKADDR_ARG)&addr,sizeof(addr));
while (1)
{
ret = read(fd,buffer,256);
printf("%d,%x\n",__LINE__,ret);
ret = write(fd,buffer,ret);
printf("%d,%x\n",__LINE__,ret);
}
close(fd);
return 0;
}
TCP server编程:
- 使用套接字socket创建数据流描述符
- 绑定本地地址bind端口
- listen本地端口
- accpet等待连接有连接后返回新创建的套接字
- 使用fork创建处理新进来的连接
- 使用read accpect返回的套接字
- 使用write accpect返回的套接字
#include "stdio.h"
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <linux/net.h>
#include <arpa/inet.h>
#include "string.h"
#define CLEAR(type) memset(&type,0,sizeof(type))
int main(int argc,char*argv[])
{
printf("%s,%d test example \n",__FILE__,__LINE__);
char buffer[256];
int len=0;
int fd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr;
CLEAR(addr);
addr.sin_addr.s_addr = inet_addr("192.168.146.129");
addr.sin_family = AF_INET;
addr.sin_port = htons(4567);
int afdret = bind(fd,(__CONST_SOCKADDR_ARG)&addr,sizeof(addr));
// CLEAR(addr);
// addr.sin_addr.s_addr = inet_addr("192.168.11.66");
// addr.sin_family = AF_INET;
// addr.sin_port = htons(6000);
int ret = listen(fd,5);
int afd=0;
while (1)
{
CLEAR(addr);
afd = accept(fd,(__SOCKADDR_ARG)&addr,&len);
if(ret!=-1){
printf("accept %s %d\n",inet_ntoa(addr.sin_addr),len);
if(fork()){
while (1)
{
ret = read(afd,buffer,256);
printf("%d,%x\n",__LINE__,ret);
ret = write(afd,buffer,ret);
printf("%d,%x\n",__LINE__,ret); /* code */
}
}
}
}
close(fd);
return 0;
}
参考资料:《linux网络编程》宋斌