質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.45%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

606閲覧

PythonでClientとServerでそれぞれ2つずつの通信ができません。

brukko

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2021/11/24 05:41

環境はMacです。バージョンはPython 2.7.18。
目指しているプログラムの内容は、以下の通りです。
TCPでクライアント-1とクライアント-2は、両方のサーバーServer-1,Server-2にデータを送信できるようにします。各クライアントの画面に、サーバーのIPアドレスとポート番号を表示します。順番としては、クライアントを一つ増やす>サーバーを一つ増やす感じです。

今現在は一つのクライアントに一つのサーバーとの通信まではうまくいっています。そこからクライアントを2つに増やそうとしているところです。しかし、ここで一つのクライアントしか反映されません。どうすればいいでしょうか?
クライアントを増やす時のコードの修正のやり方は、別のターミナルウィンドウを開き、そのウィンドウでクライアントclient-2を実行します。

TCPClient

1from thread import start_new_thread 2#import _thread 3from socket import * 4import time 5 6# Create a connect to Server 1 7server1Name = '127.0.0.1' # IP address of your server 8server1Port = 12000 9destAddress1 = (server1Name,server1Port) 10clientSocket1 = socket(AF_INET, SOCK_STREAM) 11clientSocket1.connect(destAddress1) 12 13#### Q4 14# Create a connect to Server 2 15server2Name = '127.0.0.1' # IP address of your server 16server2Port = 12000 17destAddress2 = (server1Name,server2Port) 18clientSocket2 = socket(AF_INET, SOCK_STREAM) 19clientSocket2.connect(destAddress2) 20#......................................... 21 22 23 24def connectServer(clientSocket,sentence): 25 clientSocket.send(sentence.encode()) 26 modifiedSentence = clientSocket.recv(1024) 27 print(modifiedSentence.decode()) 28 29 30#while True: 31# sentence = input("Input lowercase sentence:") 32 connectServer(clientSocket1,sentence) 33#コマンドを使用して、Server-2にデータを送信します 34 connectServer(clientSocket2,sentence) 35 36 #clientSocket.send(sentence) 37 modifiedSentence = clientSocket.recv(1024) 38 print (modifiedSentence) 39 print('Address: %f . Port: %f.' %()) 40 41clientSocket1.close() 42clientSocket2.close() 43 44#close other sockets here 45

TCPServer

1#import _thread 2from thread import start_new_thread 3from socket import * 4 5def handleClient(connectionSocket): 6 while True: 7 sentence = connectionSocket.recv(1024) 8 capitalizedSentence = sentence.upper() 9 connectionSocket.send(capitalizedSentence) 10 connectionSocket.close() 11 12serverPort = 12000 13serverSocket = socket(AF_INET,SOCK_STREAM) 14serverSocket.bind(('',serverPort)) 15serverSocket.listen(1) 16print("The server is ready to receive") 17while True: 18 connectionSocket, clientAddress = serverSocket.accept() 19 print '(Client Address,Port): ' + str(clientAddress) 20 21 22 #Note: for question-2, replace this loop by the thread call 23 #while True: 24 # sentence = connectionSocket.recv(1024) 25 # capitalizedSentence = sentence.upper() 26 # connectionSocket.send(capitalizedSentence) 27 #connectionSocket.close() 28 29 #このスレッド呼び出しでは、各クライアント用にスレッド(ソケット付き)が作成されます。 30 _thread.start_new_thread(handleClient,(connectionSocket,)) 31 ############## 32 33

Terminal

1$python TCPServer.py 2 3The server is ready to receive 4(Client Address,Port): ('127.0.0.1', 55816) 5Traceback (most recent call last): 6 File "TCPServer.py", line 31, in <module> 7 _thread.start_new_thread(handleClient,(connectionSocket,)) 8NameError: name '_thread' is not defined

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.45%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問