1# Basic example where server accepts messages from client.
2
3# importing socket library
4import socket
5
6# socket.AF_INET means we're using IPv4 ( IP version 4 )
7# socket.SOCK_STREAM means we're using TCP protocol for data transfer
8socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9
10print("Choose mode. s - server, c - client")
11mode = input("> ")
12
13if mode == "s":
14 ip = input("Your computer's ip address: ")
15 port = 80
16
17 # Binding socket to specified ip address and port
18 socket.bind((ip, port))
19
20 # This is max ammount of clients we will accept
21 socket.listen(1)
22
23 # Accepting connection from client
24 sock, addr = socket.accept()
25 print("Client connected from", addr)
26
27 # Receiving data from client
28 while True:
29 data = sock.recv(16384) # Raw data from client
30 text = data.decode('utf-8') # Decoding it
31
32 print("Message from client:", text)
33
34elif mode == "c":
35 ip = input("Server ip address: ")
36
37 # Connecting to server
38 socket.connect((ip, 80))
39
40 # Sending data
41 while True:
42 socket.send(input("Message: ").encode('utf-8'))
1####### TCP Server #######
2
3from socket import AF_INET,SOCK_STREAM,socket
4
5# AF_INET => TCP
6# SOCK_STREAM => IPv4
7
8sobj = socket(AF_INET,SOCK_STREAM)
9sobj.bind(('127.0.0.1',12345))
10sobj.listen(1)
11
12client , addr = sobj.accept()
13print("the ip is connected to server ",addr)
14
15while(True):
16
17 message = input()
18 if(message == "exit"):
19 client.close()
20 break
21 else:
22 client.send(message.encode())
23
24
25####### TCP Client #######
26
27from socket import AF_INET,SOCK_STREAM,socket
28
29sobj = socket(AF_INET,SOCK_STREAM)
30sobj.connect(('127.0.0.1',12345))
31while True:
32 message = sobj.recv(2048)
33 if(message == "exit"):
34 sobj.close()
35 break
36 elif(message.decode() == ''):
37 pass
38 else:
39 print(message.decode())
40
41
1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # The server's hostname or IP address
6PORT = 65432 # The port used by the server
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.connect((HOST, PORT))
10 s.sendall(b'Hello, world')
11 data = s.recv(1024)
12
13print('Received', repr(data))
14
1import socket # Import socket module
2
3s = socket.socket() # Create a socket object
4host = socket.gethostname() # Get local machine name
5port = 12345 # Reserve a port for your service.
6s.bind((host, port)) # Bind to the port
7
8s.listen(5) # Now wait for client connection.
9while True:
10 c, addr = s.accept() # Establish connection with client.
11 print 'Got connection from', addr
12 c.send('Thank you for connecting')
13 c.close() # Close the connection
1from socket import *
2import sys
3import json
4
5
6serverName = ""
7serverPort = 6100
8
9
10clientSocket = socket(AF_INET, SOCK_STREAM)
11clientSocket.connect((serverName, serverPort))
12
13sample_data = {
14 "Aparna" : 1,
15 "Pooja" : 2,
16 "Shreya" : 3,
17 "Tanishq" : 4
18}
19
20serialized_data = json.dumps(sample_data) #data serialized
21
22# clientSocket.send(str.encode(sample_data))
23clientSocket.send(str.encode(serialized_data))
24
25response_data = clientSocket.recv(1024)
26print("Response data from server : ", response_data.decode())
27
28clientSocket.close()
1conn, addr = s.accept()
2with conn:
3 print('Connected by', addr)
4 while True:
5 data = conn.recv(1024)
6 if not data:
7 break
8 conn.sendall(data)
9