?
Solved

Socket programming example.

Posted on 2003-12-08
14
Medium Priority
?
1,312 Views
Last Modified: 2007-12-19
Can anyone give me program examples for communicating between two machines via sockets ?
In more detail.
Machine A runs a program on socket X and machine B runs a program on socket Y.
Any data entered at program on machine A appears at machine B and vice versa.
Any language acceptable, but preferably Java.

Please note:
http://java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html
is confusing and I do not want to be pointed there unless it does actually work.
Thanks




0
Comment
Question by:dryzone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9901156
did u check this. this is pretty staright forward and easy.
http://www.linuxgazette.com/issue74/tougher.html
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9901174
this is an example in java
http://pandonia.canberra.edu.au/ClientServer/week13/javasocket.html

Sockets Programming in Java: A Tutorial
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html

Essentials of Java Programming, Part 2 (from MageLang)
http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/

Client-Server Programming with Java Sockets (lots of example code)
http://webster.cs.uga.edu/~jinhua/GIS99/sockets/


0
 
LVL 1

Author Comment

by:dryzone
ID: 9901366
Just another stupid question.
I used to run Java programs with Jar, but it does not work on newer Linux distributions.
What is the execution command used nowadays?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 24

Expert Comment

by:shivsa
ID: 9901389
java
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9901403
# javac -g Foo.java           <- to compile
and then run with
# java Foo                       <- to run
0
 
LVL 1

Author Comment

by:dryzone
ID: 9901410
Java runtime not on my system.
Which of the runtimes do you suggest.?
There are hordes and each is massive.
Any one of particular long term use?

I will look at your links.. looks promissing and will come back to you.

0
 
LVL 1

Author Comment

by:dryzone
ID: 9904203
Nope,
The Java exam[ples above just don't work for me. Non of the programs would run.
I would like to change the specification to classical languages that can be compiled with known compilers.
Lazarus, Kylix, Ada,  Pascal or C examples.  
Thanks
0
 
LVL 2

Expert Comment

by:asbharadwaj
ID: 9910435
Well
without the JRE on your system, none of the java programs would run

You can find a simple client server program here
http://www.pscode.com/vb/scripts/ShowCode.asp?lngWId=3&txtCodeId=7117

A somewhat basic tutorial on network programming here
http://users.actcom.co.il/~choo/lupg/tutorials/internetworking/internet-theory.html

The man pages should be more than enough to help you write your own programs
once you have an idea of what needs to be done

man socket,bind,listen,connect,accept,send,recv
0
 
LVL 1

Expert Comment

by:cluedon
ID: 10030367

you said any language acceptable, so here's perl.

server.pl:

#!/usr/bin/perl -w
#see http://search.cpan.org/~rgarcia/perl-5.6.2/pod/perlipc.pod

 use IO::Socket;
 use Net::hostent;              # for OO version of gethostbyaddr
 $PORT = 9000;                  # pick something not in use

 $server = IO::Socket::INET->new( Proto     => 'tcp',
                                  LocalPort => $PORT,
                                  Listen    => SOMAXCONN,
                                  Reuse     => 1);

 die "can't setup server" unless $server;
 print "[Server $0 accepting clients]\n";

 while ($client = $server->accept()) {
   $client->autoflush(1);
   print $client ">Welcome to $0; type help for command list.\n";
   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
   print $client ">Command? ";

   $line = <$client>;
   chomp($line);
    print $client "\n";
    printf "[%s: %s]\n", $hostinfo->name || $client->peerhost, $line;

     if ($line =~ /date|time/i)  { printf $client "%s\n", scalar localtime;}
     elsif ($line =~ /who/i )       { print  $client `who 2>&1`;   }
     else { print $client "unknown command: [$line]\n"; }

   close $client;
 }


Here's the client program:

#!/usr/bin/perl -w
#see http://search.cpan.org/~rgarcia/perl-5.6.2/pod/perlipc.pod

    use IO::Socket;
    unless (@ARGV > 1) { die "usage: $0 host port command..." }
    $host = shift(@ARGV);
    $port = shift(@ARGV);
    $EOL = "\015\012";
    $BLANK = $EOL x 2;
    foreach $command ( @ARGV ) {
        print "sending $command: \n";
        $remote = IO::Socket::INET->new( Proto     => "tcp",
                                         PeerAddr  => $host,
                                         PeerPort  => "$port",
                                        );
        unless ($remote) { die "cannot connect to $host" }
        $remote->autoflush(1);
        print $remote "$command\n";
        while ( <$remote> ) { print }
        print $remote "quit\n";
        close $remote;
        print "\n";
    }


To test these out you'll need to first run the server, then run the client with command line args.
for example ./client.pl localhost 9000 who date

If you have perl, but dont have some of the modules installed, use cpan.   To install moduels frm
cpan you log in as root & type:   perl -MCPAN -eshell.    That will give you a prompt, then you
just type "install module" and follow the instructions.

Note these are extremely simple examples adapted from the url above.  
0
 
LVL 1

Author Comment

by:dryzone
ID: 10031344
Thanks I will look at it.

0
 
LVL 1

Author Comment

by:dryzone
ID: 11850873
I did not get anything that works. This question is important to me but I am sure there must be something out there that works.
I changed the specification not to use Java. It simply never works for me.
It would be nice to have something in
ADA, Pascal (FPK), Lazarus or even assembler.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11879291
PAQed, with points refunded (300)

modulo
Community Support Moderator
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question