#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/wait.h> #include <sys/socket.h> #define PORT 5000 // The port which is communicate with server #define BACKLOG 10 #define LENGTH 512 // Buffer length int main () { int sockfd; // Socket file descriptor int nsockfd; // New Socket file descriptor int num; int size; int sin_size; // to store struct size // char sdbuf[LENGTH]; // Send buffer char recvbuf[LENGTH]; struct sockaddr_in addr_local; struct sockaddr_in addr_remote; char sendstr[16]= {"123456789 abcde"}; /* Get the Socket file descriptor */ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { printf ("ERROR: Failed to obtain Socket Despcritor.\n"); return (0); } else { printf ("OK: Obtain Socket Despcritor sucessfully.\n"); } /* Fill the local socket address struct */ bzero(&addr_local,sizeof(addr_local)); addr_local.sin_family = AF_INET; // Protocol Family addr_local.sin_port = htons(PORT); // Port number addr_local.sin_addr.s_addr = htons(INADDR_ANY); // AutoFill local address if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 ) { printf ("ERROR: Failed to bind Port %d.\n",PORT); return (0); } else { printf("OK: Bind the Port %d sucessfully.\n",PORT); } /* Listen remote connect/calling */ if(listen(sockfd,BACKLOG) == -1) { printf ("ERROR: Failed to listen Port %d.\n", PORT); return (0); } else { printf ("OK: Listening the Port %d sucessfully.\n", PORT); } while(1) { sin_size = sizeof(struct sockaddr_in); if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1) { printf ("ERROR: Obtain new Socket Despcritor error.\n"); continue; } else { printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr)); } printf("Now The server receiver client information.\n"); size=recv(nsockfd,recvbuf,LENGTH,0); if(size<0) { printf("read socket information error from %",inet_ntoa(addr_remote.sin_addr)); } else printf("receiver from client informationis %s",recvbuf); } close(nsockfd); } |
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define PORT 5000 // The port which is communicate with server #define LENGTH 256 // Buffer length int main(int argc, char *argv[]) { int sockfd; // Socket file descriptor int num; // Counter of received bytes struct sockaddr_in remote_addr; // Host address information char sendbuf[LENGTH]; char server_addr[]="192.168.1.114"; char writebuf[LENGTH]; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("ERROR: Failed to obtain Socket Descriptor!\n"); return (0); } /* Fill the socket address struct */ remote_addr.sin_family = AF_INET; // Protocol Family remote_addr.sin_port = htons(PORT); // Port number inet_pton(AF_INET, server_addr, &remote_addr.sin_addr); // Net Address bzero(&(remote_addr.sin_zero), 8); // Flush the rest of struct /* Try to connect the remote */ if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1) { printf ("ERROR: Failed to connect to the host!\n"); return (0); } else { printf ("OK: Have connected to the server\n"); } while(1) { printf("Please input information to send\n"); scanf("%s",&sendbuf); printf("your iunput information is %s. \n",sendbuf); send(sockfd,sendbuf,LENGTH,0); } close (sockfd); return (0); } |
歡迎光臨 Chinaunix (http://72891.cn/) | Powered by Discuz! X3.2 |