Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between two computers using python socket

I am using these two programs to communicate between two of my computers, one that I am ssh'd into and I am not returning anything on either side. It just runs without sending anything

client

import sys
from socket import socket, AF_INET, SOCK_DGRAM

SERVER_IP   = '127.0.0.1'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))

mySocket = socket( AF_INET, SOCK_DGRAM )

while True:
        mySocket.sendto('cool',(SERVER_IP,PORT_NUMBER))
sys.exit()

server

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )

print ("Test server listening on port {0}\n".format(PORT_NUMBER))

while True:
        (data,addr) = mySocket.recvfrom(SIZE)
        print data
sys.ext()

What could I be doing wrong?

like image 373
GreatGather Avatar asked Jul 05 '12 21:07

GreatGather


People also ask

What is socket writing program to communicate between two computers?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.


2 Answers

The problem is in the address of your client:

SERVER_IP   = '127.0.0.1'

You are connecting to the local machine and sending data, while your server is sitting on a different ip. You need to connect to either the servers ip or hostname.

You can verify this by making the client connect first (and fail if it cant)

...

import time

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect((SERVER_IP,PORT_NUMBER))

while True:
        mySocket.send('cool')
        time.sleep(.5)

Update from comments

Because you are on a wifi connection, that implies that both these machine are on the local network. You need to find the LAN ip address of the server, to specify it as the target.

Command-line approach to finding your IP

  • OSX/Linux: ifconfig
  • Windows: ipconfig /all
like image 76
jdi Avatar answered Oct 16 '22 11:10

jdi


This program is used for sending "small letters string" from the client and getting "capital letters" from the server

Server side

import socket

def Main():
   
    host = '192.168.0.12' #Server ip
    port = 4000

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    print("Server Started")
    while True:
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        print("Message from: " + str(addr))
        print("From connected user: " + data)
        data = data.upper()
        print("Sending: " + data)
        s.sendto(data.encode('utf-8'), addr)
    c.close()

if __name__=='__main__':
    Main()

Client side

import socket

def Main():

    host='192.168.0.13' #client ip
    port = 4005
    
    server = ('192.168.0.12', 4000)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host,port))
    
    message = input("-> ")
    while message !='q':
        s.sendto(message.encode('utf-8'), server)
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        print("Received from server: " + data)
        message = input("-> ")
    s.close()

if __name__=='__main__':
    Main()
like image 26
Barun Basnet Avatar answered Oct 16 '22 09:10

Barun Basnet