Socket Simple Communication

Source: Internet
Author: User

Original: Socket Simple communication

Rough and simple first edition, the next multi-add function right when practiced hand
/*============================================================================name:server.cauthor: KingVersion:Copyright:Your Copyright Noticedescription:hello World in C, ansi-style================================= ===========================================*/#include<stdlib.h>#include<pthread.h>#include<sys/socket.h>#include<sys/types.h>#include<netinet/inch.h>#include<arpa/inet.h>/*inet (3) Functions*/#include<stdlib.h>#include<errno.h>#include<stdio.h>#include<string.h>intHandleintPoint );intMainvoid) {intSFD, Ind;structsockaddr_in addr;structsockaddr_in clent;Charresv[1024x768], sendbuf[1024x768];Charbuf[1024x768];Char* Myaddr ="192.168.231.128";intRet//return value Settingssocklen_t lent;intpid;addr.sin_family= Af_inet;//IPv4 Internet ProtocolsAddr.sin_port= Htons (5050);//Enter the server port number hereaddr.sin_addr.s_addr=inet_addr (myaddr);//Inaddr_any indicates that the native IP//Obtain the socket descriptor, IPV4ASDprintf"socket start \ n"); SFD= Socket (Af_inet, Sock_stream,0);if(SFD <0) {printf ("socket error \ n");return-1;} printf ("bind start \ n");//Link a socket to a specified portif(Bind (SFD, (structSOCKADDR *) &addr,sizeof(structSOCKADDR)) <0) {printf ("bind error \ n");return-1;}//Listener Socketsprintf"listen start \ n");if(Listen (SFD,1024x768) <0) {printf ("Listen error \ n");return-1;} for (;;) {//accept information from the clientprintf"accept start \ n"); Memset (&clent,0,sizeof(Clent)); Lent=sizeof(clent); IND= Accept (SFD, (structSOCKADDR *) &clent, &lent);if(Ind <0) {printf ("Accept error%d \ n", Ind);return-1;} printf ("infor \ n");p rintf ("clent addr%s porit%d\n", Inet_ntop (Af_inet,&AMP;CLENT.SIN_ADDR, BUF,sizeof(BUF)), Ntohs (Clent.sin_port));p ID=fork ();if(PID = =0) {//Child ProcessClose (SFD); handle (IND);} Else if(PID <0) {//ErrorClose (IND);} Else {//Parent Process}}returnexit_success;}intHandleintPoint ) {intRetn;Charbuf[1024x768]; for (;;) {RETN= Read (point, buf,sizeof(BUF));if(Retn <0) {printf ("read error \ n"); close (point); Break;} Else if(Retn = =0) {printf ("client exit \ n"); close (point); Break;} printf ("client:%s\n", buf);if(strcmp ("Exit", buf) = =0) {printf ("exit \ n"); close (point);return 0;}}return 0;}




/*============================================================================ name:client.c Author:kin G Version:Copyright:Your Copyright notice Description:hello World in C, Ansi-style =========================== ================================================= */#include<stdio.h>#include<stdlib.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>/*inet (3) Functions*/intHandleintFD);intMainvoid) {    intNSD; Charbuf[1024x768]; Char* Myaddr ="192.168.231.128"; structsockaddr_in addr; printf ("Welcome to Echo client\n"); NSD= Socket (Af_inet, Sock_stream,0); printf ("connect start1 \ n"); //bzero (addr, sizeof (*ADDR));memset (&AMP;ADDR,0,sizeof(addr)); printf ("connect start2 \ n"); Addr.sin_family=af_inet; Addr.sin_port= Htons (5050); Addr.sin_addr.s_addr=inet_addr (MYADDR); printf ("connect start3 \ n"); if(Connect (NSD,structSOCKADDR *) &addr,sizeof(structSOCKADDR)) <0) {printf ("connect error \ n"); return-1; } Sleep (5); printf ("Handle start\n");    Handle (NSD);    Close (NSD); returnexit_success;}intHandleintFD) {    Charsendl[1024x768], rev[1024x768]; intRetn;  for (;;) {memset (Sendl,0,sizeof(Sendl)); memset (rev,0,sizeof(rev)); if(Fgets (Sendl,1024x768, stdin) = =NULL) {             Break; }        //printf"Wirte start\n");        Write (FD, Sendl, strlen (Sendl));    Read (FD, Rev,strlen (rev.)); }    return 0;}

Although writing procedures for several years, feeling or technology does not pass, so the socket from the new review,
Errors encountered during the period have not been compiled in the past:
Connect and accept
Because there is no deep understanding of the parameters of these two functions, so the wrong

int connect (int sockfd, const struct SOCKADDR *addr,
Socklen_t Addrlen);

Remember, it must be a value addrlen.

Accept socklen_t *addrlen If a pointer

These two depressed dead me, I thought is the same, the result adjusted for 1 hours.

Still need to work hard ah.

Socket Simple Communication

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.