-
Notifications
You must be signed in to change notification settings - Fork 0
1st iteration fixes #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PavelLinearB
wants to merge
3
commits into
develop
Choose a base branch
from
strings
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import socket | ||
import threading | ||
import sys | ||
|
||
#Wait for incoming data from the server | ||
#.decode is used to turn the message in bytes to a string | ||
def receive(socket, signal): | ||
while signal: | ||
try: | ||
data = socket.recv(32) | ||
print(str(data.decode('utf-8'))) | ||
except: | ||
print("You have been disconnected from the server") | ||
signal = False | ||
break | ||
|
||
#Get host and port | ||
host = input("Host: ") | ||
port = int(input("Port: ")) | ||
|
||
#Attempt connection to server | ||
try: | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.connect((host, port)) | ||
except: | ||
print("Could not make a connection to the server") | ||
input("Press enter to quit") | ||
sys.exit(0) | ||
|
||
#Create new thread to wait for data | ||
receiveThread = threading.Thread(target = receive, args = (sock, True)) | ||
receiveThread.start() | ||
|
||
#Send data to server | ||
#str.encode is used to turn the string message into bytes so it can be sent across the network | ||
while True: | ||
message = input() | ||
sock.sendall(str.encode(message)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import socket | ||
import threading | ||
|
||
#Variables for holding information about connections | ||
connections = [] | ||
total_connections = 0 | ||
|
||
#Client class, new instance created for each connected client | ||
#Each instance has the socket and address that is associated with items | ||
#Along with an assigned ID and a name chosen by the client | ||
class Client(threading.Thread): | ||
def __init__(self, socket, address, id, name, signal): | ||
threading.Thread.__init__(self) | ||
self.socket = socket | ||
self.address = address | ||
self.id = id | ||
self.name = name | ||
self.signal = signal | ||
|
||
def __str__(self): | ||
return str(self.id) + " " + str(self.address) | ||
|
||
#Attempt to get data from client | ||
#If unable to, assume client has disconnected and remove him from server data | ||
#If able to and we get data back, print it in the server and send it back to every | ||
#client aside from the client that has sent it | ||
#.decode is used to convert the byte data into a printable string | ||
def run(self): | ||
while self.signal: | ||
try: | ||
data = self.socket.recv(32) | ||
except: | ||
print("Client " + str(self.address) + " has disconnected") | ||
self.signal = False | ||
connections.remove(self) | ||
break | ||
if data != "": | ||
print("ID " + str(self.id) + ": " + str(data.decode('utf-8'))) | ||
for client in connections: | ||
if client.id != self.id: | ||
client.socket.sendall(data) | ||
|
||
#Wait for new connections | ||
def newConnections(socket): | ||
while True: | ||
sock, address = socket.accept() | ||
global total_connections | ||
connections.append(Client(sock, address, total_connections, "Name", True)) | ||
connections[len(connections) - 1].start() | ||
print("New connection at ID " + str(connections[len(connections) - 1])) | ||
total_connections += 1 | ||
|
||
def main(): | ||
#Get host and port | ||
host = input("Host: ") | ||
port = int(input("Port: ")) | ||
|
||
#Create new server socket | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.bind((host, port)) | ||
sock.listen(5) | ||
|
||
#Create new thread to wait for connections | ||
newConnectionsThread = threading.Thread(target = newConnections, args = (sock,)) | ||
newConnectionsThread.start() | ||
|
||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.