linux socket编程

来源:互联网 发布:日本现代著名作家知乎 编辑:程序博客网 时间:2024/06/08 13:59

server.c


 

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int port = 8000;

void main() {
 struct sockaddr_in sin;
 struct sockaddr_in pin;
 int sock_descriptor;
 int temp_sock_descriptor;
 int address_size;
 char buf[16384];
 int i,len;
 
 sock_descriptor = socket(AF_INET , SOCK_STREAM , 0); //定义套接口描述字
 if(sock_descriptor == -1)  {
  perror("call to socker");
  exit(1);
 }

 bzero(&sin, sizeof(sin));
 sin.sin_family  = AF_INET;
 sin.sin_addr.s_addr = INADDR_ANY;
 sin.sin_port = htons(port);

 
 if(bind(sock_descriptor, (struct sockaddr *)&sin ,sizeof(sin)) == -1 ) { //将套接口和进程联系起来
  perror("call to listen");
  exit(1);
 }
 
 if(listen (sock_descriptor , 20 )  ==  -1) {
  perror("call to listen");
  exit(1);
 } 
 
 printf("Accepting connections .../n");
 
 while(1) {
  temp_sock_descriptor = accept (sock_descriptor , (struct sockaddr *)&pin , &address_size); //监听
  if(temp_sock_descriptor == -1 ) {
   perror("call to accept");
   exit(1);
  } 
  
  if(recv( temp_sock_descriptor,buf,16384,0) == -1 ) {  //接收
   perror("call to recv");
   exit(1);
  }
  printf("received from client:%s/n",buf);
  
  len = strlen(buf);
  for(i=0; i<len ; i++) buf[i] = toupper(buf[i]);
  
  if(send(temp_sock_descriptor ,buf ,len , 0) == -1) {  //发送
   perror("call to send");
   exit(1);
  }

  close(temp_sock_descriptor);
 } 

执行结果:

[root@xing GNUlinux]# ./server
Accepting connections ...
received from client:A default test string

 


 

client.c

 


 

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

char *host_name = "127.0.0.1";
int port = 8000;

void main(int argc,char *argv[]) {
 char buf[8192];
 char message[256];
 int socket_descriptor;
 struct sockaddr_in pin;
 struct hostent *server_host_name;
 
 char *str = "A default test string";
 
 if(argc<2){
  printf("usage:' test /"Any test string/"/n");
  printf("We wull send a default test string./n");
 }else{
  str = argv[1];
 }

 if( server_host_name = gethostbyname(host_name) == 0) {
  perror("Error resolving local host/n");
  exit(1);
 }

 bzero(&pin, sizeof(pin));
 pin.sin_family = AF_INET;
 pin.sin_addr.s_addr = htonl(INADDR_ANY);
 pin.sin_addr.s_addr = ( (struct in_addr *)(server_host_name->h_addr))->s_addr;
 pin.sin_port = htons(port);

 
 if( (socket_descriptor = socket(AF_INET, SOCK_STREAM ,0 )) == -1) {
  perror("Error opening socket/n");
  exit(1);
 } 
 
 if( connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {
  perror("Error connecting to socket/n");
  exit(1);
 }
 printf("sending messag %s to server ... /n", str);
 if( send (socket_descriptor, buf, strlen(str), 0) == -1) {
  perror("Error in send/n");
  exit(1);
 }
 printf("..sent message.. wait for response.../n");
 
 if(recv(socket_descriptor, buf ,8192,0) == -1) {
  perror("Error in receiving response from server /n");
  exit(1);
 }
 
 printf("/nResponse from server:/n/n%s/n",buf);
 
 close(socket_descriptor);
}
 

执行结果:

[root@xing GNUlinux]# ./client
usage:' test "Any test string"
We will send a default test string.
sending messag A default test string to server ...
..sent message.. wait for response...

Response from server:

A DEFAULT TEST STRING
[root@xing GNUlinux]#
 

原创粉丝点击