Linux Socket Test

Source: Internet
Author: User

client:mini2440

SERVER:PC Ubuntu

The network socket data transmission is a special i/o,socket is also a kind of file descriptor. The socket also has a function similar to open file: socket (), call socket (), the function returns an integer socket descriptor, subsequent connection establishment, data transmission and other operations are also implemented through the socket. This test with mini2440 do client,pc machine to do server, at the same time connected to the LAN. After pinging, run the server.c on the mini2440 running CLIENT.C,PC.

CLIENT.C:

#include <stdlib.h> #include <sys/types.h> #include <stdio.h> #include <sys/socket.h> #include   <netinet/in.h> #include <string.h>int main () {int cfd;int recbytes;int Sin_size;char buffer[1024]={0};  struct sockaddr_in s_add,c_add;unsigned short portnum=0x8888; Port NUMBERCFD = socket (af_inet, sock_stream, 0); if ( -1 = = CfD) {printf ("Socket fail!    \ r \ n "); return-1;} Bzero (&s_add,sizeof (struct sockaddr_in)); S_add.sin_family=af_inet;s_add.sin_addr.s_addr= inet_addr ("   172.20.21.247 "); IP of your PC s_add.sin_port=htons (portnum);p rintf ("s_addr =% #x, port:% #x \ r \ n", S_add.sin_addr.s_addr,s_add.sin_    Port), if ( -1 = = Connect (CfD, (struct sockaddr *) (&s_add), sizeof (struct sockaddr))) {printf ("Connect fail!\r\n"); return-1;}    printf ("Connect OK!\r\n"), if ( -1 = = (Recbytes = read (cfd,buffer,1024))) {printf ("Read data fail!\r\n"); return-1;} Buffer[recbytes]= ';p rintf ("%s\r\n", buffer); GetChar (); close (CFD); return 0;}
SERVER.C:

#include <stdlib.h> #include <sys/types.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h>int main () {int sfp,nfp;struct sockaddr_in s_add,c_add;int sin_size;     unsigned short portnum=0x8888; Port NUMBERSFP = socket (af_inet, sock_stream, 0); if ( -1 = = SFP) {printf ("Socket fail!    \ r \ n "); return-1;} printf ("Socket OK!\r\n"); Bzero (&s_add,sizeof (struct sockaddr_in)); S_add.sin_family=af_inet;s_add.sin_addr.s_ Addr=htonl (Inaddr_any); s_add.sin_port=htons (portnum); if ( -1 = = bind (SFP, struct sockaddr *) (&s_add), sizeof (    struct sockaddr)) {printf ("Bind fail!\r\n"); return-1;}    if ( -1 = = Listen (sfp,5)) {printf ("Listen Fail!\r\n"); return-1;} while (1) {sin_size = sizeof (struct sockaddr_in); NFP = accept (SFP, struct sockaddr *) (&c_add), &sin_size); if (-1 =    = NFP) {printf ("Accept fail!\r\n"); return-1;} printf ("Waiting for% #x:% #x \ r \ n", Ntohl (C_ADD.SIN_ADDR.S_ADDR), Ntohs (C_add.sin_port)); if (-1 = = Write (NFP, "hello,socket\r\n", +)) {printf ("Write fail!\r\n"); return-1;} printf ("Write ok!\r\n"); Close (NFP);} Close (SFP); return 0;}
Compile Client.c:arm-linux-gcc-o client client.c generate client executable file

Compiling the SERVER.C:GCC-O server server.c build Server executable file

Copy Client to mini2440

PC Running Server:

[Email protected]:~#./server

Socket OK!

mini2440 Running Client:

[Root@friendlyarm/sdcard] #./client

S_addr = 0xf71514ac, port:0x8888

Connect OK!

Hello,socket

The client has received a string of "Hello,socket" issued by the server

Linux Socket Test

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.