client.c
#include"my_sort.h" //my_sort 放在include中,里面包含my_socket.h
#define MY_IP "127.0.0.1"
#define MY_PORT 6666
#define SER_IP "127.0.0.1"
#define SER_PORT 8888
int main()
{
srand(getpid());
int arr[20];
int fd_client;
int len;
my_socket(&fd_client,MY_TCP,MY_IP,MY_PORT);//绑定自己的
arr_init(arr,20);//用my_sort.c的函数初始化
printf("before:
");
arr_show(arr,20);
printf("connecting...
");
SA server_addr;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(SER_PORT);
server_addr.sin_addr.s_addr=inet_addr(SER_IP);
my_connect(fd_client,(pSA)&server_addr,sizeof(SA));
printf("connect success!
");
my_send(&len,fd_client,arr,80);//将数组发送过去
my_recv(&len,fd_client,arr,80);//接收处理好的数组
printf("after:
");
arr_show(arr,20);
return 0;
}
server.c
#include"my_sort.h"
#define IP "127.0.0.1"
#define PORT 8888
int main()
{
int fd_server,fd_client;
int arr[20];
int len;
my_socket(&fd_server,MY_TCP,IP,PORT);
my_listen(fd_server,10);
my_accept(&fd_client,fd_server,NULL,NULL);
printf("a client on!
");
my_recv(&len,fd_client,arr,80);//接收客户端的数据
printf("before:
");
arr_show(arr,20);
quick_sort(arr,20);//调用my_soet.c的快速排序函数
printf("after:
");
arr_show(arr,20);
my_send(&len,fd_client,arr,80);//处理好后发送给客户端
return 0;
}
my_sort.c
#include "my_sort.h"
void arr_init(int* arr, int len)
{
int index ;
for(index = 0 ; index < len; index ++)
{
arr[index] = rand() % 1000 ;
}
}
void arr_show(int* arr, int len)
{
int index ;
for(index = 0 ; index < len; index ++)
{
printf("%4d", arr[index]);
}
printf("
");
}
插入排序
void insert_sort(int* arr, int len)
{
int pos , index ;
int key ;
for(pos = 1 ; pos < len; pos ++)
{
key = arr[pos] ;
for(index = pos - 1 ; index >= 0; index --)
{
if(arr[index] < key)
{
break ;
}else
{
arr[index + 1] = arr[index] ;
}
}
arr[index + 1] = key ;
}
}
static void swap(int* left, int* right)
{
int tmp = *left ;
*left = *right ;
*right = tmp ;
}
//快速排序
void quik_sort(int* arr, int len)
{
if(len < 10)
{
int pos , index ;
int key ;
for(pos = 1 ; pos < len; pos ++)
{
key = arr[pos] ;
for(index = pos - 1 ; index >= 0; index --)
{
if(arr[index] < key)
{
break ;
}else
{
arr[index + 1] = arr[index] ;
}
}
arr[index + 1] = key ;
}
}else
{
int k ;
k = partion(arr, len);//0 - k-1(< key) k(key) (k+ 1 - len -1) (>= key)
quik_sort(arr, k);
quik_sort(arr + k + 1, len - k - 1);
}
}
方法1
int partion(int* arr, int len)
{
int key = arr[0] ;
int low, high ;
low = 0 ;
high = len - 1 ;
while(low < high)
{
while(low < high && arr[high] >= key )
{
high -- ;
}
if(low >= high)
{
break ;
}else
{
swap(&arr[low], &arr[high]);
}
while(low < high && arr[low] < key)
{
low ++ ;
}
if(low < high)
{
swap(&arr[low], &arr[high]);
}
}
return low ;
}
方法2 快慢指针
#include "my_sort.h"
int partion2(int *arr,int len)
{
int fast,last,key;
last=0;
fast=1;
key=arr[0];
for(;fast<len;fast++)
{
if(arr[fast]<key)
{
swap(&arr[last+1],&arr[fast]);
last++;
}
}
swap(&arr[0],&arr[last]);
return last;
}
上面的三个文件都包含#include "my_sort.h"
我们来看看my_sort.h
#include"my_socket.h"
void arr_init(int* arr,int len);
void arr_show(int* arr,int len);
void insert_sort(int* arr,int len);
static void swap(int* left,int* right);
void quick_sort(int* arr,int len);
int partion(int* arr,int len);
原来它里面已经包含my_socket.h
那就追踪看看my_socket.h
#ifndef __MY_SOCKET_H__ #define __MY_SOCKET_H__ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #define IN #define OUT #define IN_OUT #define MY_TCP 1 #define MY_UDP 2 typedef struct sockaddr* pSA ; typedef struct sockaddr_in SA ; #define MY_ASSERT(flag,msg) ( (flag) ? NULL : ( fprintf(stdout,msg), exit(EXIT_FAILURE) ) ) // NULL代表什么也不做 void my_socket(OUT int *local_sfd, int protocal, char *local_ip, int local_port); void my_listen(int local_sfd, int backlog); void my_accept(OUT int *peer_sfd, int local_sfd, OUT pSA peer_addr, IN_OUT int *addr_len ); void my_connect(int local_sfd, pSA peer_addr, int addr_len); void my_recv(OUT int *recv_len, int peer_sfd, IN_OUT void *base, int len); void my_send(OUT int *send_len, int peer_sfd, void *base, int len); void my_recvfrom(OUT int *recvfrom_len, int peer_sfd, IN_OUT void *base, int len, OUT pSA peer_addr, IN_OUT int *addr_len); void my_sendto(OUT int *sendto_len, int peer_sfd, OUT void *base, int len, pSA peer_addr, int addr_len); void my_close(int sfd); #endif
//编译
gcc -o c client.c my_sort.c -l my_socket -I /tmp/include
gcc -o s server.c my_sort.c -l my_socket -I /tmp/include
//运行结果如下