0% found this document useful (0 votes)
9 views3 pages

TCP Array Sorting

a tcp array sorting code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

TCP Array Sorting

a tcp array sorting code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// tcp -server array sorting

#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>

void sort_array(int *arr,int size)


{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

void main()
{
int s,sock;
struct sockaddr_in server,client;
int a[20],b[20],i,j,k,n;

socklen_t len = sizeof(client);

if((s = socket(AF_INET,SOCK_STREAM,0)) < 0)


{
perror("socket failed");
exit(EXIT_FAILURE);
}

server.sin_family = AF_INET;
server.sin_port = htons(6000);
server.sin_addr.s_addr = inet_addr("127.0.0.1");

if( bind(s,(struct sockaddr*)&server,sizeof(server)) < 0 )


{
perror("bind failed");
exit(EXIT_FAILURE);
}

if( listen(s,5) < 0)


{
perror("listen failed");
exit(EXIT_FAILURE);
}

printf("server is running \n");

if((sock = accept(s,(struct sockaddr*)&client,&len)) < 0)


{
perror("accept failed");
exit(EXIT_FAILURE);
}

recv(sock,&n,sizeof(int),0);
printf("size of array is : %d\n",n);

recv(sock,a,sizeof(int)*n,0);

printf("Recvieved array is : \n");

for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}

sort_array(a,n);

printf("\nThe sorted array is : \n");


for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}

send(sock,a,sizeof(int)*n,0);
printf("\nSorted array sent to client\n");

close(sock);
close(s);
}

//tcp - array sorting client

#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>

void main()
{
int sock;
struct sockaddr_in server;
int a[20],b[20],i,n;

if((sock = socket(AF_INET,SOCK_STREAM,0)) < 0 )


{
perror("socket failed");
exit(EXIT_FAILURE);

server.sin_family = AF_INET;
server.sin_port = htons(6000);
server.sin_addr.s_addr = inet_addr("127.0.0.1");

if(connect(sock,(struct sockaddr*)&server,sizeof(server))<0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}

printf("Enter the size of array: ");


scanf("%d",&n);

printf("Enter the array: ");


for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
send(sock,&n,sizeof(int),0);
send(sock,a,sizeof(int) * n,0);

recv(sock,b,sizeof(int)*n,0);

printf("\nThe recived sorted array form server is: \n");


for(i=0;i<n;i++)
{
printf("%d ",b[i]);
}
printf("\n");

send(sock,&n,sizeof(int),0);
send(sock,a,sizeof(int)*n,0);

close(sock);

You might also like