www.pudn.com > C_socket_for_solaris.rar > csockclient.c, change:2006-12-07,size:2263b


/** 
 * @(#)csockclient.c 1.0 Aug 17, 2006 
 * Copyright (c) 2006 SIEMENS/PSE. All Rights Reserved 
 * Copying of this document or code and giving it to others and the  
 * use or communication of the contents thereof, are forbidden without 
 * expressed authority. Offenders are liable to the payment of damages. 
 * All rights reserved in the event of the grant of a patent or the  
 * registration of a utility model, design or code 
 * Issued by SIEMENS Program and System Engineering (Nanjing) Co., Ltd. 
 */ 
  
/* 
   @Author      Ren JiaWei 
   comment: this is just a demo for MCS socket, when programmer write the real  
            procedure in MCS circumstance, the programmer should call the API of 
            mcfs_reader and  mcfs_writer, and then replace the part of SQL in this 
            API.  
   --------------------------- 
*/ 
#include <stdio.h> 
#include <errno.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include "sock_typedef.h" 
 
void str_echo(int sock) 
{ 
    ssize_t n; 
    int MAXLINE =1024; 
    char    buf[MAXLINE]; 
    int i=0; 
    memset(buf,0,MAXLINE); 
    for(;;) 
    { 
        if((n = read(sock, buf, MAXLINE))>0) 
        {        	 
            printf("the c get str len:%d\n",strlen(buf)); 
            for(i=strlen(buf); i>=0; i--) 
            { 
                int c = buf[i]; 
                if(c==13||c==10) 
                { 
                    buf[i] = '\0'; 
                }	 
            } 
             
            strcat(buf,":c client test"); 
            buf[strlen(buf)]='\n'; 
            buf[strlen(buf)]='\0'; 
            send(sock,buf, strlen(buf),MSG_DONTWAIT);             
        } 
        memset(buf,0,MAXLINE); 
          
          
    } 
} 
 
int main(int argc, char ** argv) 
{ 
    int sockfd; 
    struct sockaddr_in servaddr; 
     
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    bzero(&servaddr, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = inet_addr(getenv(IP_ADDRESS)); 
    servaddr.sin_port = htons (atoi(getenv(SERVER_PORT))); 
     
    connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); 
    str_echo(sockfd); 
    exit(0); 
}