All pastes #967493 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-02 13:55 UTC
rendered paste body

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <asm/ioctls.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <signal.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>

int nodelay = 1;
#define HOST_BUF_LEN 256
int init_client(int port, char *ip) {
        int sd;
        static struct sockaddr_in addr ;
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(ip);
        if ((sd = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0) {
                perror("socket");
        }
               
        if(connect(sd,(struct sockaddr *)&addr,sizeof(addr)) < 0) {
                perror("connect");
                return(-1);
        }
        if (fcntl(sd, F_SETFL, O_NONBLOCK) < 0) {
                perror("fcntl");
                goto errout;
        }
        if (setsockopt(sd, SOL_SCTP, SCTP_NODELAY, &nodelay, sizeof(nodelay)) < 0) {
                perror("setsockopt");
                goto errout;
        }
        return sd;
        errout:
                close(sd);
                return(-1);
}   
void print_param(int sd) { 
        char buf[HOST_BUF_LEN];
        int read_b;
       while( read_b = read(STDIN_FILENO, &buf,  sizeof(buf)) ){
                 perror("read");         
        //while((read_b = fread(&buf, 1, sizeof(buf), ))>0){
               if ((send(sd, buf, read_b, 0)) == -1) {
                        perror("send");
                }
                perror("send");
        }
       sleep(1);
        perror("before close");
        close (sd);
        perror("");

}
int usage(int argc){
        if(argc != 3) {
                printf ("usage: client dest_ip\n");
                return (-1);
        }
        return 1;
}
int main(int argc,char *argv[]) {
        if(usage(argc) > 0 ){
                int sock;
                if ((sock = init_client(atoi(argv[1]), argv[2])))       print_param(sock) ;
        }
        return 1;
}