A simple example of Linux socket

Source: Internet
Author: User
Tags socket error htons

Client: dl_client.c
# Include "unp. H"

Int main (INT argc, char * argv [])
{
Char recvbuff [50], buff [50], * P;
Int sockfd, Len, RET, test;
Struct sockaddr_in serveraddr;
File * FP;

If (argc! = 3 ){
Printf ("input:./client <IPaddress> <FILENAME>/N ");
Return 0;
}

If (sockfd = socket (af_inet, sock_stream, 0) <0 ){
Printf ("socket error/N ");
Return 0;
}

Bzero (& serveraddr, sizeof (serveraddr ));
Serveraddr. sin_family = af_inet;
Serveraddr. sin_port = htons (serv_port );
If (inet_ton (af_inet, argv [1], & serveraddr. sin_addr) <= 0 ){
Printf ("inet_ton error for % s/n", argv [1]);
Return 0;
}

If (connect (sockfd, (Sa *) & serveraddr, sizeof (serveraddr) <0 ){
Printf ("Connect error1/N ");
Return 0;
}

Bzero (buff, sizeof (buff ));
Strcpy (buff, argv [2]);
Send (sockfd, buff, sizeof (buff), 0 );
Bzero (buff, sizeof (buff ));
Sprintf (buff, "./DL _ % s", argv [2]);
Recv (sockfd, recvbuff, sizeof (recvbuff), 0 );
Len = 0;
Ret = 0;
If (LEN = atoi (recvbuff)> 0 ){
P = (char *) malloc (LEN + 1 );
Bzero (p, Len + 1 );
Test = Recv (sockfd, P, Len, 0 );
Printf ("*** test = % d/N", test );
Printf ("*** Len = % d/N", Len );
If (FP = fopen (buff, "W + "))! = NULL ){
Ret = fwrite (p, Len, 1, FP );
Fclose (FP );
}
Free (P );
}
If (Len! = 0 & ret = 1)
Printf ("Download % s OK! /N ", argv [2]);
Else printf ("Download error! /N ");

Close (sockfd );

Return 0;
}
Server: dl_server.c
# Include "unp. H"

Int socket (INT family, int type, int Protocol)
{
Int ret;
Ret = socket (family, type, Protocol );
If (Ret <0 ){
Printf ("socket error/N ");
}
Return ret;
}

Void BIND (INT sockfd, const struct sockaddr * myaddr, socklen_t addrlen)
{
If (BIND (sockfd, myaddr, addrlen) <0)
Printf ("BIND error/N ");
}

Void listen (int fd, int backlog)
{
Char * PTR;
If (PTR = getenv ("listenq "))! = NULL)
Backlog = atoi (PTR );
If (Listen (FD, backlog) <0)
Printf ("Listen error/N ");
}

Int fork ()
{
If (Fork () = 0)
Return 0;
Else
Return-1;
}

Int main (INT argc, char * argv [])
{
Char recvbuff [50], path [50], * P = NULL;
Int listensock, connsock, RET, Len;
Pid_t childpid;
Socklen_t clilen;
Struct sockaddr_in servaddr, cliaddr;
File * FP;

Listensock = socket (af_inet, sock_stream, 0 );

Bzero (& servaddr, sizeof (servaddr ));
Servaddr. sin_family = af_inet;
Servaddr. sin_addr.s_addr = htonl (inaddr_any );
Servaddr. sin_port = htons (serv_port );

BIND (listensock, (Sa *) & servaddr, sizeof (servaddr ));

Listen (listensock, listenq );

For (;;){
Clilen = sizeof (cliaddr );
Connsock = accept (listensock, (Sa *) & cliaddr, & clilen );

If (childpid = fork () = 0 ){
Bzero (recvbuff, sizeof (recvbuff ));
Bzero (path, sizeof (PATH ));
Recv (connsock, recvbuff, sizeof (recvbuff), 0 );
Sprintf (path, "./% s", recvbuff );

Ret =-1;
If (FP = fopen (path, "R "))! = NULL ){
Fseek (FP, 0, seek_end );
Len = ftell (FP );
P = (char *) malloc (LEN + 1 );
Bzero (p, Len + 1 );
Fseek (FP, 0, seek_set );
Ret = fread (p, Len + 1, 1, FP );
Fclose (FP );
}
If (Ret! =-1 ){
Bzero (path, sizeof (PATH ));
Sprintf (path, "% d", Len );
Send (connsock, path, sizeof (PATH), 0 );
Send (connsock, P, Len + 1, 0 );
}
Else {
Len = 0;
Bzero (path, sizeof (PATH ));
Sprintf (path, "% d", Len );
Send (connsock, path, sizeof (PATH), 0 );
}
Free (P );

Close (listensock );
Return 0;
}
Close (connsock );
}
}
Included header file: unp. H (the header file obtained from the project may be different)
# Include <getopt. h>
# Include <resolv. h>
# Include <dirent. h>
# Include <stdarg. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/time. h>
# Include <time. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Include <errno. h>
# Include <fcntl. h>
# Include <netdb. h>
# Include <signal. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <sys/STAT. h>
# Include <sys/uio. h>
# Include <unistd. h>
# Include <sys/Wait. H>
# Include <sys/UN. h>

# Ifdef have_sys_select_h
# Include <sys/select. h>
# Endif

# Ifdef have_sys_sysctl_h
# Include <sys/sysctl. h>
# Endif

# Ifdef have_poll_h
# Include <poll. h>
# Endif

# Ifdef have_sys_event_h
# Include <sys/event. h>
# Endif

# Ifdef have_strings_h
# Include <string. h>
# Endif

# Ifdef have_sys_ioctl_h
# Include <sys/IOCTL. h>
# Endif

# Ifdef have_sys_filio_h
# Include <sys/filio. h>
# Endif

# Ifdef have_sys_sockio_h
# Include <sys/sockio. h>
# Endif

# Ifdef have_pthread_h
# Include <pthread. h>
# Endif

# Ifdef have_ent_if_dl_h
# Include <net/if_dl.h>
# Endif

# Ifdef have_netinet_sctp_h
# Include <netinet/sctp. h>
# Endif

# Ifdef _ OSF _
# UNDEF Recv
# UNDEF send
# Define Recv (A, B, C, D) recvfrom (a, B, c, d, 0, 0)
# Define send (A, B, C, D) sendto (a, B, c, d, 0, 0)
# Endif

# Ifndef inaddr_none
# Define inaddr_none 0 xffffffff
# Endif

# Ifndef shut_rd
# Define shut_rd 0
# Define shut_wr 1
# Define shut_rdwr 2
# Endif

# Ifndef inet_addrstrlen
# Define inet_addrstrlen 16
# Endif

# Ifndef inet6_addrstrlen
# Define inet_addrstrlen 46
# Endif

# Ifndef have_bzero
# Define bzero (PTR, n) memset (PTR, 0, n)
# Endif

# Ifndef have_gethostbyname2
# Define gethostbyname2 (host, family) gethostbyname (host ))
# Endif

Struct unp_in_pktinfo {
Struct in_addr ipi_addr;
Int ipi_ifindex;
};

# Ifndef cmsg_len
# Define cmsg_len (size)
# Endif

# Ifndef cmsg_space
# Define cmsg_space (size)
# Endif

# Ifndef sun_len
# Define sun_len (SU) (sizeof (* (SU)-sizeof (SU)-> sun_path) + strlen (SU)-> sun_path ))
# Endif

# Ifndef af_local
# Define af_local af_unix
# Endif
# Ifndef pf_local
# Define pf_local pf_unix
# Endif

# Ifndef inftim
# Define inftim (-1)
# Ifdef have_poll_h
# Define inftim_unph
# Endif
# Endif

# Define listenq 1024

# Define maxline 4096
# Define buffsize 8192

# Define serv_port 9877
# Define maid "9877"
# Define unixstr_path "/tmp/Unix. Str"
# Define unixdg_path "/tmp/Unix. DG"

# Define SA struct sockaddr

# Define have_struct_sockaddr_storage
# Ifndef have_struct_sockaddr_storage

# DEFINE _ ss_maxsize 128
# DEFINE _ ss_alignsize (sizeof (int64_t ))
# Ifdef have_sockaddr_sa_len
# DEFINE _ ss_pad1size (_ ss_alignsize-sizeof (u_char)-sizeof (sa_family_t ))
# Else
# DEFINE _ ss_pad1size (_ ss_alignsize-sizeof (sa_family_t ))
# Endif
# DEFINE _ ss_pad2size (_ ss_maxsize-2 * _ ss_alignsize)

Struct sockaddr_storage {
# Ifdef have_sockaddr_sa_len
U_char ss_len;
# Endif
Sa_family_t ss_family;
Char _ ss_pad1 [_ ss_pad1size];
Int64_t _ ss_align;
Char _ ss_pad2 [_ ss_pad2size];
};
# Endif

# Define file_mode (s_irusr | s_iwusr | s_irgrp | s_iroth)
# Define dir_mode (file_mode | s_ixusr | s_ixgrp | s_ixoth)

Typedef void sigfunc (INT );

# Define min (A, B) (a) <(B )? (A): (B ))
# Define max (A, B) (a)> (B )? (A): (B ))

# Ifndef have_addrinfo_struct
// # Include "../lib/addrinfo. H"
# Endif

# Ifndef have_if_nameindex_struct
Struct if_nameindex {
Unsigned int if_index;
Char * if_name;
};
# Endif

Compile and run the client and server respectively. A simple data transmission is OK!

Also from csdn! I have practiced it myself! No problem! However! Pay attention to the Linux system LAN Settings!

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.