Code:
/***************************************************************************
main.c - description
-------------------
begin : Wed Aug 4 13:45:50 WIT 2004
copyright : (C) |YEAR| by Ru2004awan
email : rgunawan@yahoo.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "unp.h"
main(int argc, char **argv)
{
char server_addr[30];
int commsock, md, mst, port;
if (argc <>
fprintf(stderr, "Usage: client_socket_standard
return;
} else {
strcpy(server_addr, argv[1]);
sscanf(argv[2], "%d", &port);
//sscanf(argv[3], "%d", &md);
//sscanf(argv[4], "%d", &mst);
init_sock(server_addr, port, &commsock);
send_to_socket(commsock);
//nb_send(commsock, md, mst);
close(commsock);
}
}
init_sock(char *server_addr, int port, int *commsock)
{
struct sockaddr_in servaddr;
int x;
/********************* Initialisasi client socket *****************/
*commsock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
inet_aton(server_addr, &servaddr.sin_addr);
x = connect(*commsock, (SA *) &servaddr, sizeof(servaddr));
fprintf(stderr, "server connected commsock = %d\n", *commsock);
/******************************************************************/
}
send_to_socket(int commsock)
{
unsigned char tmp[1000], buf[1000], repbuf[1000];
int nread, nwrite, l;
while (1) {
fprintf(stderr, "Start of loop\n ");
fgets(buf, 200, stdin);
l = strlen(buf);
nwrite = write(commsock, buf, l);
fprintf(stderr, "write %d bytes to server\n", nwrite);
nread = read(commsock, repbuf, 1000);
repbuf[nread] = 0;
if (!nread) {
fprintf(stderr, "server connection close\n");
break;
} else
fprintf(stderr, "server_reply : %s\n", repbuf);
}
}
Header File unp.h
#ifndef __unp_h
#define __unp_h
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// #ifdef HAVE_SYS_SELECT_H
#include
// #endif
// #ifdef HAVE_POLL_H
#include
// #endif
// #ifdef HAVE_STRING
#include
// #endif
// #ifdef HAVE_SYS_IOCTL_H
#include
// #endif
// #ifdef HAVE_SYS_FILIO_H
// #include
// #endif
// #ifdef HAVE_SYS_SOCKIO_H
// #include
// #ifdef HAVE_PTHREAD_H
#include
// #endif
#ifdef __osf__
#undef recv
#undef send
#define recv(a,b,c,d) recvfrom(a,b,c,d,0,0)
#define send(a,b,c,d) sendto(a,b,c,d,0,0)
#endif
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
#ifndef SHUT_RD
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
#endif
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif
#ifndef HAVE_BZERO
#define bzero(ptr,n) memset(ptr,0,n)
#endif
#ifndef HAVE_GETHOSTBYNAME2
#define gethostbyname2(host,family) gethostbyname((host))
#endif
// struct in_pktinfo {
// struct in_addr ipi_addr;
// int ipi_ifindex;
// };
#ifndef CMSG_LEN
#define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size))
#endif
#ifndef CMSG_SPACE
#define CMSG_SPACE(size) (sizeof(struct cmsgdhr) + (size))
#endif
#ifndef SUN_LEN
#define SUN_LEN(su) \
(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
#endif
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
#ifndef INFTIM
#define INFTIM (-1)
// #ifdef HAVE_POLL_H
// ##define INFTIM_UNPH
// #endif
#endif
#define LISTENQ 1024
#define MAXLINE 4096
#define MAXSOCKADDR 128
#define BUFFSIZE 8192
#define SERV_PORT 9877
#define SERV_PORT_STR *9877*
#define UNIXSTR_PATH */tmp/unix.str*
#define UNIXDG_PATH */tmp/unix.dg*
#define SA struct sockaddr
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
typedef void Sigfunc (int);
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
// #ifndef HAVE_ADDRINFO_STRUCT
// #ifndef HAVE_TIMESPEC_STRUCT
//struct timespec {
//time_t tv_sec;
//long tv_nsec;
//};
#endif
Tidak ada komentar:
Posting Komentar