//tcpclient.c
#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
int main(){
struct sockaddr_in addr;
int addr_len;
long sFlag;
int sockfd;
int ret;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if ( sockfd == -1 ) {
printf("create socket error\n");
}
sFlag = 1;
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char*)&sFlag,sizeof(sFlag))!=0) {
printf("reuses fail %d\n\r",errno);
}
/* fill address info */
addr_len = sizeof(struct sockaddr_in);
memset(&addr, 0, addr_len);
addr.sin_family = AF_INET;
addr.sin_port = htons(8886);
addr.sin_addr.s_addr = inet_addr("192.168.1.7");
fd_set w;
FD_ZERO(&w);
FD_SET(sockfd,&w);
ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
if(ret == -1){
perror("connect");
exit(-1);
}
if(-1!=write(sockfd,"00",2));
{
printf("发送成功\n");
}
close(sockfd);
}
//tcpserver.c
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(){
int sockfd,clifd;
struct sockaddr_in addr ,cliaddr;
int addr_len;
long sFlag;
int ret;
char buf[3];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if ( sockfd == -1 ) {
printf("create socket error\n");
}
sFlag = 1;
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char*)&sFlag,sizeof(sFlag))!=0) {
printf("reuses fail %d\n\r",errno);
}
/* fill address info */
addr_len = sizeof(struct sockaddr_in);
memset(&addr, 0, addr_len);
addr.sin_family = AF_INET;
addr.sin_port = htons(8886);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ( bind(sockfd, (struct sockaddr *)&addr, addr_len) != 0 ) {
printf("bind socket error\n");
}
if ( listen(sockfd, 1) != 0 ) {
printf("listen error\n");
}
/* set Non-blocking Mode */
sFlag = fcntl(sockfd,F_GETFL,0);
if ( fcntl(sockfd, F_SETFL, sFlag|O_NDELAY) == -1 ) {
printf("fcnt err\n");
}
fd_set r;
FD_ZERO(&r);
FD_SET(sockfd,&r);
ret=select(sockfd+1,&r,NULL,NULL,0);
printf("ret = %d\n",ret);
if(ret>0)
{
addr_len = sizeof(cliaddr);
if(FD_ISSET(sockfd,&r))
clifd = accept(sockfd,(struct sockaddr *)&cliaddr,&addr_len);
if(clifd == -1){
perror("accpet");
exit(-1);
}
ret = recv(clifd,buf,3,0);
printf("ret = %d\n",ret);
if(ret>0)
printf("%s\n",buf);
}
close(sockfd);
}
udpclient.c
//客户
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define NUM 100
int main() {
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
int res=0,i=0;
if (-1 == sockfd) {
perror("socket");
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8886);//都是服务器的
addr.sin_addr.s_addr = inet_addr("192.168.1.10");
for(i=0;i<100;i++){
usleep(1000*1000/25);
res = sendto(sockfd,"00",2,0,(struct sockaddr*)&addr,sizeof(addr));
printf("res=%d\n",i);}
close(sockfd);
}
//udpserver.c
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(){
int sockfd,clifd;
struct sockaddr_in addr ,cliaddr;
int addr_len,socket_len;
long sFlag;
int ret;
unsigned char buf[8];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if ( sockfd == -1 ) {
printf("create socket error\n");
}
sFlag = 1;
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char*)&sFlag,sizeof(sFlag))!=0) {
printf("reuses fail %d\n\r",errno);
}
/* fill address info */
addr_len = sizeof(struct sockaddr_in);
memset(&addr, 0, addr_len);
addr.sin_family = AF_INET;
addr.sin_port = htons(8886);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ( bind(sockfd, (struct sockaddr *)&addr, addr_len) != 0 ) {
printf("bind socket error\n");
}
/* set Non-blocking Mode */
sFlag = fcntl(sockfd,F_GETFL,0);
if ( fcntl(sockfd, F_SETFL, sFlag|O_NDELAY) == -1 ) {
printf("fcnt err\n");
}
fd_set r;
FD_ZERO(&r);
FD_SET(sockfd,&r);
while(1){
memset(buf, 0,8);
ret=select(sockfd+1,&r,NULL,NULL,0);
if(ret>0)
{
socket_len = sizeof(cliaddr);
if(FD_ISSET(sockfd,&r))
{ret = recvfrom(sockfd,&buf,9,0,(struct sockaddr*)&cliaddr
,&socket_len);
if(ret<0) continue;
printf("%s\n",buf);
}
}
}
close(sockfd);
}