Question

In: Computer Science

TCP client and server using C programming I am having trouble on how to read in...

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

Solutions

Expert Solution

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()



Related Solutions

Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
Greetings, Consider a client server model.The server sends the message 'I am the server' to client....
Greetings, Consider a client server model.The server sends the message 'I am the server' to client. Describe and compare in details how the client and server exchange these messages using internet domain in the following two modes. a) connection-oriented modes b) connectionless-oriented modes
How do I make a simple TCP python web client and web server using only "import...
How do I make a simple TCP python web client and web server using only "import socket"? Basically, the client connects to the server, and sends a HTTP GET request for a specific file (like a text file, HTML page, jpeg, png etc), the server checks for the file and sends a copy of the data to the client along with the response headers (like 404 if not found, or 200 if okay etc). The process would be: You first...
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks....
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks. #include <iostream> #include <string> #include <cstdlib> using namespace std; /* * structure to store employee details * employee details are stored as linked list */ struct Employee { private:    string full_name; //full name    double net_income; //income public:    struct Employee *next; //pointing to the next employee details in the list                        /*                   ...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
I was able to calculate (a) but I am having trouble with the calculations of (b)....
I was able to calculate (a) but I am having trouble with the calculations of (b). Thanks! The following are New York closing rates for A$/US$ and $/£:                                     A$/$ = 1.5150;               $/£ = $1.2950             (a) Calculate the cross rate for £ in terms of A$ (A$/£).             (b) If £ is trading at A$1.95/£ in London (cross market) on the same day, is there an arbitrage opportunity?  If so, show how arbitrageurs with £ could profit from this opportunity and calculate the arbitrage...
what will be the code in C programming for the client and server chat application for...
what will be the code in C programming for the client and server chat application for the below issue :- write the C Programming code that able client have a unique ID to be known by the server
I am having trouble figuring out how to To-Do list using java. Can anyone guide me...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT