TCP Header for Echo Communication


 
Thread Tools Search this Thread
Top Forums Programming TCP Header for Echo Communication
# 1  
Old 02-27-2005
TCP Header for Echo Communication

I am attempting to construct a TCP header for a simple echo server program using header fields: type, reserve1, reserve2, data length, sequence number, and data. Type will be a char variable whereas chracter 8 will represent an echo request message and 0 will represent an echo reply message. The reserved fields will be of value zero for now. Data will of course be arbitrary text.

I am wanting the server to be able to take a client request and interpret the type field of the header to see if it is indeed an echo request(8). If so, the server will then construct a reply header, change the Type field to 0, and copy the data length, sequence number, and data fields from the client header. I want the client to then receive the reply header and also interpret the type field to determine if it is an echo reply message(0). If it is, the client will echo the data, sequence number, and data length.

I am at a point now where I am stuck and was hoping to get some assistance. I am thinking the client is setup properly except for the last print statement, but I am not exactly sure what to do on the server end. Any help would be greatly appreciated. Here is what I have for the client so far:

Code:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


#define SERVER_PORT 12345
#define BUF_SIZE 4096


int main(int argc, char *argv[])
{
  int sock;
  int connection;
  int message_sent;
  int message_rec;
  char user_data[BUF_SIZE];
  char host_reply[BUF_SIZE];
  char *ip_server;
  char *string_name;
  char datagram[4096];
  struct sockaddr_in ip_address;

  ip_server = argv[1];
  string_name = argv[2];


  // Constructing TCP Header
  struct tcphder {
    char type;
    int reserve1;
    short reserve2;
    int len;
    int sequence;
    char *data;
  };
  
  memset (datagram, 0, 4096);

  struct tcphder *tcph = (struct tcphder *)datagram;

  tcph->data = argv[2];
  tcph->sequence = random();
  tcph->len = strlen(argv[2]);
  tcph->reserve1 = 0;
  tcph->reserve2 = 0;
  tcph->type = '8';

  if (argc != 3)
  {
    fprintf(stderr, "Usage: %s <Server IP Address> <Message>\n", argv[0]);
    exit(1);
  }


// Create a communication point
  sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (sock < 0)
  {
    fprintf(stderr, "socket error.\n");
    exit(1);
  }


// Build server adress structure
  memset(&ip_address, 0, sizeof(ip_address));
  ip_address.sin_family=AF_INET;
  ip_address.sin_addr.s_addr=inet_addr(ip_server);
  ip_address.sin_port=htons(SERVER_PORT);

// Make connection to server
  connection = connect(sock, (struct sockaddr *) &ip_address, sizeof(ip_address));

  if (connection < 0)
  {
    fprintf(stderr, "connect error.\n");
    exit(1);
  }


  // Send entered data to server
  message_sent = send(sock, datagram, tcph->len, 0);

  message_rec = recv(sock, host_reply, 1024, 0);
  host_reply[message_rec] = '\0';

  printf("Reply from server: %s\n", host_reply);

close(sock);
exit(1);

}

For the server: (The server is currently just setup for basic echo communication)

Code:
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define SERVER_PORT 12345
#define BUFFER_SIZE 4096
#define MAX_CONNECT 5


int main(int argc, char *argv[])
{
   int sock_server;
   int sock_client;
   int on=1;
   int binding;
   int listening;
   int message_rec;
   int message_sent;
   char buffer_size[BUFFER_SIZE];
   char message_client[1024];
   socklen_t len;
   struct sockaddr_in ip_address_server;
   struct sockaddr_in ip_address_client;



// Create socket for incomming connection
   sock_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (sock_server < 0)
  {
 fprintf(stderr, "socket error\n");
    exit(1);
  }


// Build server address structure
   memset(&ip_address_server, 0, sizeof(ip_address_server));
   ip_address_server.sin_family = AF_INET;
   ip_address_server.sin_addr.s_addr = htonl(INADDR_ANY);
   ip_address_server.sin_port = htons(SERVER_PORT);
   setsockopt(sock_server, SOL_SOCKET, SO_REUSEADDR, (char *) &on,   sizeof(on));


// Attach a local address to a socket
   binding = bind(sock_server, (struct sockaddr *) &ip_address_server, sizeof(ip_address_server));

  if (binding != 0)
  {
    fprintf(stderr, "bind error\n");
    exit(1);
  }


// Listen for connection
   listening = listen(sock_server, MAX_CONNECT);
   printf ("listening");
  if (listening != 0)
  {
    fprintf(stderr, "listen error\n");
    exit(1);
  }
while (1)
{

   // Accept client connection
   sock_client = accept(sock_server, &ip_address_client, &len);

   // Get message from client
   message_rec = recv(sock_client, buffer_size, BUFFER_SIZE, 0);   
   buffer_size[message_rec] = '\0';

   // Send message back to client
   message_sent = send(sock_client, buffer_size, strlen(buffer_size), 0);


}
   close(sock_client);

}

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Solaris

Too much TCP retransmitted and TCP duplicate on server Oracle Solaris 10

I have problem with oracle solaris 10 running on oracle sparc T4-2 server. Os information: 5.10 Generic_150400-03 sun4v sparc sun4v Output from tcpstat.d script TCP bytes: out outRetrans in inDup inUnorder 6833763 7300 98884 0... (2 Replies)
Discussion started by: insatiable1610
2 Replies

3. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

4. IP Networking

To get tcp header alone in log

To get tcp header alone in log ----------------------------------------------------------------------------------------# vi /etc/sysconfig/iptables ---added the following 2 lines.. -A INPUT -j LOG --log-ip-options -A INPUT -j LOG --log-tcp-options ]# vi /etc/syslog.conf ---added the... (1 Reply)
Discussion started by: linuxadmin
1 Replies

5. UNIX for Dummies Questions & Answers

Echo file name as header

I'm trying to create a header row that contains the filename of a file for all columns. e.g. file1.txt first middle last 1 2 3 4 5 6 desired output file should be: file1 file1 file1 first middle last 1 2 3 4 5 6 my code is: for file in *.txt; do echo... (3 Replies)
Discussion started by: calitiggr
3 Replies

6. Shell Programming and Scripting

Perl script for TCP communication

Hi I need to write perl script for TCP communication over server and client using IO::Select and the method select ( READ, WRITE, EXCEPTION ). I have not much idea on networking.. Please Help... Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies

7. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies

8. Programming

How to Parse TCP header

I am using socket programming and want to parse TCp header. What are the required header files and the reqiured functions Plz help me out... Vipin (2 Replies)
Discussion started by: cool_vip
2 Replies

9. Programming

the performance impaction of TCP communication in same machine?

Two processes at the same machine communicaton with each other use TCP. The communicaton flow between them is very heavy. My question is How the impaction of "processes at the same machine communicaton " .If I mv one process to another machine and send the same data, the impact is less than... (1 Reply)
Discussion started by: chenhao_no1
1 Replies
Login or Register to Ask a Question