C-language TCP and UDP clients and servers

Source: Internet
Author: User
Tags fread sin strcmp strtok htons

Are the simplest to remember.

This is my git address: HTTPS://GITHUB.COM/YANJINYUN/CLANGUAGETCPUDP

The simplest server for TCP:

intMainintargcConst Char*argv[]) {    intLISTENFD, ACCEPTFD; structsockaddr_in sin, cin;    Socklen_t Clen; Charbuf[1024x768];    Signal (SIGCHLD, sig_ign); LISTENFD= Socket (pf_inet, Sock_stream,0); Sin.sin_family=pf_inet; Sin.sin_port= Htons (Atoi (argv[2])); Sin.sin_addr.s_addr= Inet_addr (argv[1]); Bind (LISTENFD, (structSOCKADDR *) &sin,sizeof(sin)); Listen (LISTENFD,5);  while(1) {Clen=sizeof(CIN); printf ("listen ... \ n"); ACCEPTFD= Accept (LISTENFD, (structSOCKADDR *) &cin, &Clen); printf ("Client%s%d connect\n", Inet_ntoa (CIN.SIN_ADDR), Ntohs (Cin.sin_port)); if(fork () = =0) {close (LISTENFD);  while(1) {bzero (buf,sizeof(BUF)); if(Recv (ACCEPTFD, buf,sizeof(BUF),0) ==0) {printf ("Client%s%d exit\n", Inet_ntoa (CIN.SIN_ADDR), Ntohs (Cin.sin_port)); Exit (0); } printf ("recv (%s%d):%s\n", Inet_ntoa (CIN.SIN_ADDR), Ntohs (Cin.sin_port), buf);    }} close (ACCEPTFD); }    return 0;}

The simplest client for TCP:

intMainintargcConst Char*argv[]) {    intCFD = socket (pf_inet, Sock_stream,0); structsockaddr_in cin; Charbuf[1024x768]; Cin.sin_family=pf_inet; Cin.sin_port= Htons (Atoi (argv[2])); Cin.sin_addr.s_addr= Inet_addr (argv[1]); Connect (CFD, (structSOCKADDR *) &cin,sizeof(CIN)); printf ("Connect IP (%s) port (%s) server ok\n", argv[1], argv[2]); if(fork () = =0)    {         while(1) {bzero (buf,sizeof(BUF)); Recv (CfD, buf,sizeof(BUF),0); printf ("recv:%s\n", BUF); }    }     while(1) {fgets (buf,sizeof(BUF), stdin); Buf[strlen (BUF)-1] =' /'; Send (CFD, buf, strlen (BUF)+1,0); }    return 0;}

The simplest server for UDP:

int main (int argc, const char *argv[]) {int sfd;struct sockaddr_in sin, Cin;char buf1[1024], buf2[1024], Buf3[1024];socklen _t CLEN;SFD = socket (pf_inet, SOCK_DGRAM, 0); sin.sin_family = Pf_inet;sin.sin_port = Htons (atoi (argv[2)); Sin.sin_ ADDR.S_ADDR = inet_addr (argv[1]), Bind (SFD, (struct sockaddr *) &sin, sizeof (SIN)), if (fork () = = 0) {while (1) {Clen = sizeof (CIN); bzero (buf1, sizeof (BUF1)); Recvfrom (SFD, buf1, sizeof (BUF1), 0, (struct sockaddr *) &cin, &clen); printf ("Recvfrom:%s%d%s\n", Inet_ntoa (CIN.SIN_ADDR), Ntohs (Cin.sin_port), BUF1);}} while (1) {fscanf (stdin, "%s%s%s", Buf1, Buf2, buf3); cin.sin_family = Pf_inet;cin.sin_port = Htons (atoi (BUF2)); Cin.sin_ ADDR.S_ADDR = inet_addr (BUF1); SendTo (SFD, buf3, strlen (buf3) +1, 0, (struct sockaddr *) &cin, sizeof (CIN));} return 0;}

The simplest client for UDP:

int main (int argc, const char *argv[]) {int sfd;struct sockaddr_in Cin;char buf1[1024], buf2[1024], buf3[1024];socklen_t cl EN;SFD = socket (pf_inet, SOCK_DGRAM, 0), if (fork () = = 0) {while (1) {Clen = sizeof (CIN); bzero (buf1, sizeof (BUF1)); Recvfrom ( SFD, buf1, sizeof (BUF1), 0, (struct sockaddr *) &cin, &clen);p rintf ("Recvfrom:%s%d%s\n", Inet_ntoa (cin.sin_addr ), Ntohs (Cin.sin_port), BUF1);}} while (1) {fscanf (stdin, "%s%s%s", Buf1, Buf2, buf3); cin.sin_family = Pf_inet;cin.sin_port = Htons (atoi (BUF2)); Cin.sin_ ADDR.S_ADDR = inet_addr (BUF1); SendTo (SFD, buf3, strlen (buf3) +1, 0, (struct sockaddr *) &cin, sizeof (CIN));} return 0;}

The simplest server for ftp:

#define N 256void list (int connectfd) {DIR *dir;struct dirent *d;char buf[n];if ((Dir=opendir ((const char *) Get_current_ Dir_name ())) = = NULL) {perror ("Opendir"); exit (1);} while (D=readdir (dir)) = NULL) {if (d->d_name[0] = = '. ') {continue;} strcpy (buf, d->d_name); Send (CONNECTFD, buf, N, 0);} Closedir (dir);} void get (int connectfd, char *filename) {char buf[n]; FILE *fp;int n;if ((fp=fopen (filename, "r") = = NULL) {strcpy (buf, "no"), Send (CONNECTFD, buf, sizeof (BUF), 0);} strcpy (buf, "yes"); Send (CONNECTFD, buf, sizeof (BUF), 0), while (N=fread (buf, 1, sizeof (BUF), FP)) > 0) {Send (connectfd , buf, n, 0);} Fclose (FP);} void put (int connectfd, char *filename) {char buf[n]; FILE *fp;int n;if ((fp=fopen (filename, "r") = = NULL) {strcpy (buf, "no"); Send (CONNECTFD, buf, sizeof (BUF), 0); fp = fopen (fi Lename, "W"), while ((N=recv (CONNECTFD, buf, sizeof (BUF), 0)) > 0) {fwrite (buf, 1, N, FP);} Fclose (FP); return;} strcpy (buf, "yes"); Send (CONNECTFD, buf, sizeof (BUF), 0);} int main (int argc, const char *argv[]) {FILE *fp;iNT N, LISTENFD, CONNECTFD, i;struct sockaddr_in s, c;struct servent *sport;socklen_t Clen;char *LINEP = NULL, Buf[N], *arg [32];signal (SIGCHLD, sig_ign); if ((Fp=fopen ("/etc/ftp.conf", "r") = = NULL) {perror ("fopen"); exit (1);} if (Getline (&LINEP, &n, fp) = =-1) {perror ("getline"); exit (0);} Linep[strlen (LINEP)-1] = ' sport=getservbyname '; chdir (LINEP); if ((Myftp ("Perror", "tcp") = = = NULL) {getservbyname ); exit (1);} #ifdef debugprintf ("%d%s\n", N, Linep), #endifif ((Listenfd=socket (pf_inet, Sock_stream, 0)) = =-1) {perror ("socket"); Exit (1);} s.sin_family = Pf_inet;s.sin_port = sport->s_port; S.SIN_ADDR.S_ADDR = htonl (Inaddr_any); if (Bind (LISTENFD, (struct sockaddr *) &s, sizeof (s)) = =-1) {perror ("bind"); Exit (1);} Listen (LISTENFD, 5); while (1) {clen = sizeof (c); if (Connectfd=accept (LISTENFD, (struct sockaddr *) &c, &clen) = = -1) {perror ("accept"); exit (1);} if (fork () = = 0) {close (LISTENFD); recv (CONNECTFD, buf, sizeof (BUF), 0); Arg[0] = Strtok (buf, ""); for (I=1; Arg[i-1]!=null; i++) {Arg[i]=strtok (NULL, "");} if (strcmp (arg[0], "list") = = 0) {list (CONNECTFD);} if (strcmp (arg[0], "get") = = 0) {get (CONNECTFD, arg[1]);} if (strcmp (arg[0], "put") = = 0) {put (CONNECTFD, arg[1]);} Close (CONNECTFD); exit (0);} Close (CONNECTFD);}}

  

  

FTP is the simplest client device:

#define N 256void list (int connectfd) {DIR *dir;struct dirent *d;char buf[n];if ((Dir=opendir ((const char *) Get_current_ Dir_name ())) = = NULL) {perror ("Opendir"); exit (1);} while (D=readdir (dir)) = NULL) {if (d->d_name[0] = = '. ') {continue;} strcpy (buf, d->d_name); Send (CONNECTFD, buf, N, 0);} Closedir (dir);} void get (int connectfd, char *filename) {char buf[n]; FILE *fp;int n;if ((fp=fopen (filename, "r") = = NULL) {strcpy (buf, "no"), Send (CONNECTFD, buf, sizeof (BUF), 0);} strcpy (buf, "yes"); Send (CONNECTFD, buf, sizeof (BUF), 0), while (N=fread (buf, 1, sizeof (BUF), FP)) > 0) {Send (connectfd , buf, n, 0);} Fclose (FP);} void put (int connectfd, char *filename) {char buf[n]; FILE *fp;int n;if ((fp=fopen (filename, "r") = = NULL) {strcpy (buf, "no"); Send (CONNECTFD, buf, sizeof (BUF), 0); fp = fopen (fi Lename, "W"), while ((N=recv (CONNECTFD, buf, sizeof (BUF), 0)) > 0) {fwrite (buf, 1, N, FP);} Fclose (FP); return;} strcpy (buf, "yes"); Send (CONNECTFD, buf, sizeof (BUF), 0);} int main (int argc, const char *argv[]) {FILE *fp;iNT N, LISTENFD, CONNECTFD, i;struct sockaddr_in s, c;struct servent *sport;socklen_t Clen;char *LINEP = NULL, Buf[N], *arg [32];signal (SIGCHLD, sig_ign); if ((Fp=fopen ("/etc/ftp.conf", "r") = = NULL) {perror ("fopen"); exit (1);} if (Getline (&LINEP, &n, fp) = =-1) {perror ("getline"); exit (0);} Linep[strlen (LINEP)-1] = ' sport=getservbyname '; chdir (LINEP); if ((Myftp ("Perror", "tcp") = = = NULL) {getservbyname ); exit (1);} #ifdef debugprintf ("%d%s\n", N, Linep), #endifif ((Listenfd=socket (pf_inet, Sock_stream, 0)) = =-1) {perror ("socket"); Exit (1);} s.sin_family = Pf_inet;s.sin_port = sport->s_port; S.SIN_ADDR.S_ADDR = htonl (Inaddr_any); if (Bind (LISTENFD, (struct sockaddr *) &s, sizeof (s)) = =-1) {perror ("bind"); Exit (1);} Listen (LISTENFD, 5); while (1) {clen = sizeof (c); if (Connectfd=accept (LISTENFD, (struct sockaddr *) &c, &clen) = = -1) {perror ("accept"); exit (1);} if (fork () = = 0) {close (LISTENFD); recv (CONNECTFD, buf, sizeof (BUF), 0); Arg[0] = Strtok (buf, ""); for (I=1; Arg[i-1]!=null; i++) {Arg[i]=strtok (NULL, "");} if (strcmp (arg[0], "list") = = 0) {list (CONNECTFD);} if (strcmp (arg[0], "get") = = 0) {get (CONNECTFD, arg[1]);} if (strcmp (arg[0], "put") = = 0) {put (CONNECTFD, arg[1]);} Close (CONNECTFD); exit (0);} Close (CONNECTFD);}}

C-language TCP and UDP clients and servers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.