| ioPAC RTU Controllers C/C++ Sample Code Programming Guide

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions
client_tcp_echo.c File Reference

Client TCP Echo More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <errno.h>`
`#include <string.h>`
`#include <netdb.h>`
`#include <unistd.h>`
`#include <termios.h>`
`#include <netinet/in.h>`
`#include <sys/types.h>`
`#include <sys/socket.h>`
`#include <arpa/inet.h>`

## Macros
#define | BUFFER_SIZE 32

## Functions
int | ipCheck (char *ip)
int | main (int argc, char *argv[])

## Detailed Description

Client TCP Echo

Copyright
  Moxa Inc.

Date
  04-10-2013

Author
  Eddy Kao

Version
  V1.0
  Client TCP Echo

    Introduction:
    Server will echo each byte which is sended by Client.
    Example:
    1. Using default: ./client_tcp_echo
    2. Setting port: ./client_tcp_echo -p1234
    Default:
    Port = 2323
    Help:

    root@Moxa:/tmp#./client_tcp_echo -h
    Echo Server, client program.

    Usage: ./client_tcp_echo [OPTIONS]

    Options:
            -i       IP
            -p       Port [0-65535]. Default port = 2323

## Macro Definition Documentation

#define BUFFER_SIZE 32

## Function Documentation

int ipCheck | ( | char * | ip | ) |

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

/*******************************************************************************
* Copyright Moxa Inc.
*
* Echo Server(client)
*
* Date Author Comment
* 04-10-2013 Eddy Kao Created.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <termios.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 32
int ipCheck(char *ip)
{
int count = 3;
char *pch;
pch = ip;
while(count)
{
pch = strstr(pch, ".");
if(pch == NULL) return -1;
pch++;
count--;
}
return 0;
}
int main(int argc, char* argv[])
{
int i = 0;
int rc = 0;
char ch = 0;
char chIgnore = 0;
char ip[17];
char ipFlag = 0;
unsigned short port = 2323; //Default port
char receiveBuf[BUFFER_SIZE];
char sendBuf[BUFFER_SIZE];
int sockfd = 0;
int nBytes = 0;
struct sockaddr_in sockAddress;
memset(ip, 0, sizeof(ip));
while((rc = getopt(argc, argv, "hi:p:")) != -1)
{
switch(rc)
{
case 'i':
if(ipCheck(optarg))
{
printf("Invalid IP\r\n");
exit(1);
}
strcpy(ip, optarg);
ipFlag = 1;
break;
case 'p':
port = atoi(optarg);
break;
case '?':
case 'h':
default:
printf("Echo Server, client program.\n\n");
printf("Usage: ./client_tcp_echo [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s IP\n", "-i");
printf("\t%-8s Port [%d-%d]. Default port = %d\n", "-p", 0, 65535, port);
printf("\n");
return 0;
}
}
if(ipFlag == 0)
{
printf("No IP\r\n");
exit(1);
}
//send a ~ z
memset(sendBuf, 0, sizeof(sendBuf));
for(i = 0; i < 26; i++) sendBuf[i] = 'a' + i;
sendBuf[26] = '\0';
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) //TCP connection
{
perror("socket");
exit(1);
}
sockAddress.sin_family = AF_INET;
sockAddress.sin_port = htons(port);
sockAddress.sin_addr.s_addr = inet_addr(ip);
bzero(&(sockAddress.sin_zero), 8);
if(connect(sockfd, (struct sockaddr*)&sockAddress, sizeof(struct sockaddr)) == -1)
{
close(sockfd);
perror("connect");
exit(1);
}
while(1)
{
// Send
nBytes = write(sockfd, sendBuf, strlen(sendBuf));
if(nBytes == -1 || nBytes == 0)
{
close(sockfd);
printf("Server is closing...\r\n");
break;
}
printf("Send %d bytes\r\n", nBytes);
// Receive
memset(receiveBuf, 0, sizeof(receiveBuf));
nBytes = read(sockfd, receiveBuf, sizeof(receiveBuf));
if(nBytes == -1 || nBytes == 0)
{
close(sockfd);
printf("Server is closing...\r\n");
break;
}
printf("Receive %d bytes, data = %s\r\n", nBytes, receiveBuf);
printf("Please keyin 'c' to send again or 'q' to exit.\r\n");
ch = getchar();
while(1)
{
chIgnore = getchar();
if(chIgnore == 0xa) break;
}
if(ch == 'c') continue;
else if(ch == 'q')
{
close(sockfd);
break;
}
else
{
close(sockfd);
printf("Unknow characters...\r\n");
break;
}
}
return 0;
}