In: Computer Science
TCP client and server using C programming
I am having trouble on how to read in the IP adress and port number from the terminal
Example: Enter IP address: 127.0.0.1
Enter Port Number: 8000
in both client and server code. How do can I make I can assign the Ip address and port number using the example above.
the error I get is that the client couldn't connect with the server whenever i get the port number from the user in the terminal
BELOW IS THE PYTHON IMPLEMENTATION OF SOCKET PROGRAMMING FOR BOTH CLIENT AND SERVER
CLIENT SIDE:
import socket
port=8000
s=socket.socket( socket.AF_INET,socket.SOCK_STREAM,0 )
s.setsockopt( socket.SOL_SOCKET,socket.SO_REUSEADDR,1 )
s.connect(( '127.0.0.1' ,port ))
while 1:
print("client>>\n")
msg=raw_input()
if msg=="quit":
break
s.send(msg)
data=s.recv(50)
print(data)
if data=="quit":
break
s.close()
SERVER SIDE:
import socket
port = 8000
s = socket.socket( socket.AF_INET,socket.SOCK_STREAM,0 )
s.setsockopt( socket.SOL_SOCKET,socket.SO_REUSEADDR,1 )
s.bind(( '',port ))
s.listen( 5 )
print(" i am the Server !, May I help you ?!")
conn, addr = s.accept()
while 1:
data = conn.recv(500)
print("Client >> " + str(data))
msg = raw_input()
msg = "Server >> " + msg
conn.send(msg)
if data == "quit":
break
if msg == "quit":
break
conn.close()