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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

2234閲覧

Javaで、サーバーとクライアントでデータを送り合う処理

anpan___

総合スコア28

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

1グッド

1クリップ

投稿2020/03/12 05:10

現在Javaでオセロを作成しております。
その中で『プレイヤー1(サーバー)VSプレイヤー2(クライアント)』
でプログラムを作成したいと思っております。

具体的には
基本的なルールはサーバーで実行。
クライアントが行うのは、配置する座標を決めてサーバーに返す処理です。

クライアントはサーバーを継承してコーディングを行なっています。
どのようなメソッドで値を返すことができるのかわからずご質問させていただきました。

また、教えていただきたいと感じている点をまとめるとこの通りです。
・座標となるpointをサーバーに返す処理
・どちらかのターンの時に反対のコードを座標が帰ってくるまで待機させる

かなり途中のコードですが、下記の中の
pointをサーバーに返したいと考えております。

Java

1import java.io.BufferedReader; 2import java.io.IOException; 3import java.io.InputStreamReader; 4import java.io.PrintWriter; 5import java.net.Socket; 6import java.util.ResourceBundle; 7 8 /** 9 * ソケット通信(クライアント側) 10 */ 11 class Clients extends Servers{ 12 void runClient() { 13 ResourceBundle rb = ResourceBundle.getBundle("Pro"); 14 Socket cSocket = null; 15 BufferedReader in = null; 16 try { 17 p2 = new Player('b', 2);//プレイヤー1 白 18 cSocket = new Socket(rb.getString("server_id"), Integer.parseInt(rb.getString("server_port"))); 19 in = new BufferedReader(new InputStreamReader(cSocket.getInputStream())); 20 PrintWriter out = new PrintWriter(cSocket.getOutputStream(), true); 21 System.out.println("サーバーと接続中"); 22 while (true) { 23 for (int i = 0; i < board.length; i++) { 24 for (int j = 0; j < board[i].length; j++) { 25 if (board[i][j] == 0) {//コマが置かれていない場所をチェック 26 if (Board.check_change(board, i, j, p2.color, "check")) {//置ける時 27 board[i][j] = 3; 28 } 29 } 30 } 31 } 32 Board.print(kazu, board, com2); 33 for (int i = 1; i < status.length; i++) {//盤面の状態を表示 34 System.out.println(color[i] + ":" + status[i] + " "); 35 } 36 System.out.println("------------------------"); 37 System.out.println(color[p2.color] + "のターン" + " "); 38 int[] point = p2.getPoint();//一番有利な座標を見つける。 39 col = point[0]; 40 row = point[1]; 41 } //mainループ 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } finally { 45 try { 46 if (in != null) { 47 in.close(); 48 } 49 if (cSocket != null) { 50 cSocket.close(); 51 } 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 } 58 59 public class Client { 60 public static void main(String[] args) { 61 Clients s2 = new Clients(); 62 s2.runClient(); 63 } 64 } 65
s.k👍を押しています

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

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

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

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

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

dodox86

2020/03/12 05:38

JavaのTCPサーバークライアントプログラムが分からないということでしょうか。 (1) サーバーからクライアントへの受信データは in = new BufferedReader(new InputStreamReader(cSocket.getInputStream())); でのinで読み出し、 (2) クライアントからサーバーへの送信データは PrintWriter out = new PrintWriter(cSocket.getOutputStream(), true); のout で書き出す、と言うことが基本になりそうですが。
jimbe

2020/03/12 06:32

機能的に考えますと, クライアントがサーバを継承する意味は無いように思えますが, なぜ継承しているのでしょうか.
gentaro

2020/03/12 07:16

概念的にもクライアントがサーバーを継承するのは理解し難い気が。 P2P的な何かを実装しようとしてる場合ならギリギリわかる気はするけど。
guest

回答1

0

ベストアンサー

このようなコードで試せます.
(無限ループなので, テキトウに止めてください.)

java

1package teratail.q246663; 2 3import java.io.*; 4import java.net.*; 5 6public class ReversiCommunicationTest { 7 public static final int PORT = 12345; 8 public static void main(String[] args) { 9 new Thread(new Server(PORT)).start(); 10 new Client("127.0.0.1", PORT).run(); 11 } 12} 13class Point { 14 final int col, row; 15 Point(int col, int row) { this.col = col; this.row = row; } 16 @Override 17 public String toString() { 18 return super.toString()+"[col="+col+",row="+row+"]"; 19 } 20} 21class Communicator implements AutoCloseable { 22 private BufferedReader reader; 23 private PrintWriter out; 24 Communicator(Socket s) throws IOException { 25 try { 26 reader = new BufferedReader(new InputStreamReader(s.getInputStream())); 27 out = new PrintWriter(s.getOutputStream(), true); 28 } catch(IOException e) { 29 close(); 30 } 31 } 32 void send(Point point) throws IOException { 33 out.println(point.col+","+point.row); 34 } 35 Point receive() throws IOException { 36 String line = reader.readLine(); 37 if(line == null) return null; 38 String[] tokens = line.split(","); 39 return new Point(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])); 40 } 41 @Override 42 public void close() throws IOException { 43 if(out == null) { out.close(); out = null; } 44 if(reader == null) { reader.close(); reader = null; } 45 } 46} 47class Server implements Runnable { 48 private int port; 49 Server(int port) { 50 this.port = port; 51 } 52 @Override 53 public void run() { 54 try (ServerSocket ss = new ServerSocket(port); 55 Communicator comm = new Communicator(ss.accept());) { 56 57 Point point = null; 58 while(true) { 59 System.out.println("Server: 考え中"); 60 point = thinking(); 61 System.out.println("Server: 送信="+point); 62 comm.send(point); 63 64 System.out.println("Server: 受信待ち"); 65 point = comm.receive(); 66 System.out.println("Server: 受信="+point); 67 } 68 } catch(IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 private Point thinking() { 73 try { 74 Thread.sleep((long)(Math.random()*3000)+2000); //[ms] 75 } catch(InterruptedException e) { 76 e.printStackTrace(); 77 } 78 return new Point((int)(Math.random()*8), (int)(Math.random()*8)); 79 } 80} 81class Client implements Runnable { 82 private String host; 83 private int port; 84 Client(String host, int port) { 85 this.host = host; 86 this.port = port; 87 } 88 @Override 89 public void run() { 90 try (Communicator comm = new Communicator(new Socket(host, port));) { 91 92 Point point = null; 93 while(true) { 94 System.out.println("Client: 受信待ち"); 95 point = comm.receive(); 96 System.out.println("Client: 受信="+point); 97 98 System.out.println("Client: 考え中"); 99 point = thinking(); 100 System.out.println("Client: 送信="+point); 101 comm.send(point); 102 } 103 } catch(IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 private Point thinking() { 108 try { 109 Thread.sleep((long)(Math.random()*3000)+2000); //[ms] 110 } catch(InterruptedException e) { 111 e.printStackTrace(); 112 } 113 return new Point((int)(Math.random()*8), (int)(Math.random()*8)); 114 } 115}

plain

1Client: 受信待ち 2Server: 考え中 3Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@334495b1[col=3,row=2] 4Server: 受信待ち 5Client: 受信=teratail.q246663.ReversiCommunicationTest$Point@7852e922[col=3,row=2] 6Client: 考え中 7Client: 送信=teratail.q246663.ReversiCommunicationTest$Point@4e25154f[col=5,row=8] 8Client: 受信待ち 9Server: 受信=teratail.q246663.ReversiCommunicationTest$Point@76460dbd[col=5,row=8] 10Server: 考え中 11Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@28d2af42[col=1,row=7] 12Server: 受信待ち 13Client: 受信=teratail.q246663.ReversiCommunicationTest$Point@70dea4e[col=1,row=7] 14Client: 考え中 15Client: 送信=teratail.q246663.ReversiCommunicationTest$Point@5c647e05[col=4,row=3] 16Client: 受信待ち 17Server: 受信=teratail.q246663.ReversiCommunicationTest$Point@47fadae9[col=4,row=3] 18Server: 考え中 19Server: 送信=teratail.q246663.ReversiCommunicationTest$Point@508426ba[col=3,row=8] 20Server: 受信待ち 21 : 22 :

投稿2020/03/13 19:33

編集2020/03/14 08:16
jimbe

総合スコア12678

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

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

anpan___

2020/04/03 04:01

ご返信遅くなり大変申し訳ありません! いただいたコードを参考に無事に座標を送る処理が完成しました…! 感謝申し上げます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問