Powered by Invision Power Board


  Reply to this topicStart new topicStart Poll

> TCP Header for Echo Server
AusTex
Posted: Feb 26 2005, 09:11 AM
Quote Post


Newbie
*

Group: Members
Posts: 3
Member No.: 836
Joined: 26-February 05



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, strlen(datagram), 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.
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming/Languages | Next Newest »

Topic Options Reply to this topicStart new topicStart Poll

 



[ Script Execution time: 0.1678 ]   [ 12 queries used ]   [ GZIP Enabled ]




Partners: Cambridge Plus :: <Link Available> :: 3D Mechanical Design :: <Link Available>
Unix Man Pages / Linux Man Pages :: HiFi Forum :: SIP VoIP Phone & Provider Reviews :: UNIX/Linux Forum Archives

More info on advertising on Unix/Linux Forum