原创 socket 简单编程步骤及代码(GNU/linux编程指南中的例子)

2009-7-20 09:23 3177 7 7 分类: 软件与OS

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 = toupper(buf);
  
  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]#

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条