473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple TCP/IP server/client - help!

Hello

I am very new to perl, however i managed to make this server/client work with udp, now i would like to do the same thing but with TCP/IP, what must i do?

Server:

Expand|Select|Wrap|Line Numbers
  1.  #!perl -w
  2. # Server Program
  3. use IO::Socket::INET;
  4. print ">> Server Program <<\n";
  5.  
  6. # Create a new socket
  7. $MySocket=new IO::Socket::INET->new(
  8.     LocalPort=>1234,
  9.     Proto=>'udp'
  10. );
  11.  
  12.  
  13. # Keep receiving messages from client
  14. $def_msg="\nReceiving message from client.....\n";
  15. $sum=0;
  16.  
  17. while(1)
  18. {
  19.         $MySocket->recv($number,128);
  20.         $sum=$sum+$number;
  21.         $MySocket->send($sum);
  22.  
  23.     if($number ne '0')
  24.     {
  25.         print "\nReceived message '", $number,"'\n";
  26.  
  27.     }
  28.     # If client message is empty exit
  29.     else
  30.     {
  31.         print "Cilent has exited!";
  32.         $MySocket->send($number);
  33.         exit 1;
  34.     }
  35. }
Client:
Expand|Select|Wrap|Line Numbers
  1. #!perl -w
  2. # Client Program
  3. use IO::Socket::INET;
  4. print ">> Client Program <<";
  5.  
  6. # Create a new socket
  7. $MySocket=new IO::Socket::INET->new(
  8.     PeerPort=>1234,
  9.     Proto=>'udp',
  10.     PeerAddr=>'localhost'
  11.     );
  12.  
  13. # Send messages
  14. $def_msg="client: ";
  15. print "\n",$def_msg;
  16. while($msg=<STDIN>)
  17. {
  18.     chomp $msg;
  19.     if($msg ne '0')
  20.     {
  21.  
  22.        if(print $MySocket($msg))
  23.         {
  24.             print "";
  25.         }
  26.         if($MySocket->recv($text,128))
  27.         {
  28.             print "\server: '", $text,"'\n";
  29.             print $def_msg;
  30.         }
  31.     }
  32.     else
  33.     {
  34.         # Send an empty message to server and exit
  35.         $MySocket->send('0');
  36.         exit 1;
  37.     }
  38. }
  39.  
  40.  
  41.  
Feb 7 '09 #1
2 5388
KevinADC
4,059 Expert 2GB
Did you try changing the protocol?

Proto=>'udp'
Feb 7 '09 #2
Icecrack
174 Expert 100+
First Off in the Client script on line 28.

Expand|Select|Wrap|Line Numbers
  1. print "\server: '", $text,"'\n";
  2.  
Should be:
Expand|Select|Wrap|Line Numbers
  1. print "Server: '", $text,"'\n";
  2.  

now for server code you don't need the var or line of:

Expand|Select|Wrap|Line Numbers
  1. $def_msg="\nReceiving message from client.....\n";
  2.  
if you want to use it add
Expand|Select|Wrap|Line Numbers
  1. print $def_msg;
in between the while and if statement.
Feb 9 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Johannes Eble | last post by:
Hello Python community, I am trying the echo-client and echo-server examples in Chapter 10, "Programming Python" by Mark Lutz. It is probably the most simple sockets sample: A socket server just...
3
by: MBW | last post by:
The following code is for a simple server/client asplication that allows the user to toggle between serve and or client modes and send/recieve a message however i am getting an Attribute error on...
1
by: Chris | last post by:
Hi, how do I develop the following 'simple' program using ASP.NET webforms : a client has a text-file (aaa.txt) that has to be manipulated by code located on the server, the server would then...
10
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's...
7
by: Steven | last post by:
Hello, First, let me state that I am trying to learn asp.net, so I am a beginner. Now on to the issue. I have a webform with a single Textbox and a FileSystemWatcher monitoring a directory for...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
0
by: Harley | last post by:
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
0
by: Grzegorz Smith | last post by:
Hi All. I 'm learning ZSI to use SOAP and I desperately need help. I'm working on example from tutorial -(examples/server/send_response/ simple/wsdl/). Here are my wsdl files...
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.