LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2004, 07:43 PM   #1
devit
Member
 
Registered: Jan 2004
Distribution: Slackware 9.1
Posts: 82

Rep: Reputation: 15
problems with TCP client


I have a Slackware 9.1 box and I tried to make a simple TCP server and his client. The server has to receive a string and return it to the client. The server works well but not the client.

Hoping this will help, I posted the code of the client:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>

extern int errno;

int port;

int main (int argc, char *argv[])
{
  int sd;
  struct sockaddr_in server;
  char buffer[100];

  if (argc != 3) {
    printf ("Sintaxa: %s <adresa_server> <port>\n", argv[0]);
    return -1;
  }

  port = atoi (argv[2]);

  if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
    perror ("Eroare la socket().\n");
    return errno;
  }

  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl (inet_addr(argv[1]));
  server.sin_port = htons (port);

  if (connect (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1) {
    perror ("Eroare la connect().\n");
    return errno;
  }

  bzero (buffer, 100);
  printf ("Introduceti mesajul: ");
  fflush (stdout);
  read (0, buffer, 100);

  if (write (sd, buffer, 100) <= 0) {
    perror ("Eroare la write() spre server.\n");
    return errno;
  }

  if (read (sd, buffer, 100) < 0) {
    perror ("Eroare la read() de la server.\n");
    return errno;
  }

  printf ("Mesajul primit este: \"%s\".\n", buffer);

  close (sd);
}
When I start the client I receive this error:
Code:
bash-2.05b$ ./client-tcp 127.0.0.1 8081
Eroare la connect().
: Network is unreachable
bash-2.05b$
I tried ping 127.0.0.1 and there is no problem there. Could you help me!

Thanks!
 
Old 05-29-2004, 08:08 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Just guessing, but is it possible that you have 127.0.0.1 firewalled (for port 8081)?
 
Old 05-29-2004, 10:35 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Try setting server up like this:

Code:
    server.sin_family = PF_INET;
    server.sin_port = htons(port);
    inet_aton(argv[1], &server.sin_addr);
The 3rd argument to connect() should also be sizeof(struct sockaddr_in) instead of sizeof(struct sockaddr).

Also, bzero() is deprecated. Best to use memset(buffer, 0, 100);

Last edited by itsme86; 05-29-2004 at 10:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithreaded TCP Client examples daveseff Programming 1 03-31-2005 11:29 PM
tcp/ip programming client side gajaykrishnan Linux - Networking 2 07-29-2004 11:58 PM
how to do testing TCP/IP client/sever appl Madhukar Linux - Networking 0 05-26-2004 09:01 AM
Tcp/ip Client Server Problem tushar123 Programming 2 03-31-2004 05:16 PM
Server/Client-commucation via TCP-Sockets cYbORg Programming 5 05-18-2003 06:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration