他のアプリとの連係 - Siv3D/Reference-JP GitHub Wiki
# include <Siv3D.hpp>
void Main()
{
TCPServer server;
server.startAccept(50000);
Point friendPos(0, 0);
Window::SetTitle(L"TCPServer: 接続待機中...");
while (System::Update())
{
if (server.isConnected())
{
Window::SetTitle(L"TCPServer: 接続完了!");
server.send(Mouse::Pos());
while (server.read(friendPos));
}
if (server.hasError())
{
server.disconnect();
Window::SetTitle(L"TCPServer: 再接続待機中...");
server.startAccept(50000);
}
Circle(Mouse::Pos(), 30).draw(Palette::Skyblue);
Circle(friendPos, 10).draw(Palette::Orange);
}
}
# include <Siv3D.hpp>
void Main()
{
TCPClient client;
client.connect(IPv4::localhost(), 50000);
Point friendPos(0, 0);
Window::SetTitle(L"TCPClient: 接続待機中...");
while (System::Update())
{
if (client.isConnected())
{
Window::SetTitle(L"TCPClient: 接続完了!");
client.send(Mouse::Pos());
while (client.read(friendPos));
}
if (client.hasError())
{
client.disconnect();
Window::SetTitle(L"TCPClient: 再接続待機中...");
client.connect(IPv4::localhost(), 50000);
}
Circle(Mouse::Pos(), 30).draw(Palette::Skyblue);
Circle(friendPos, 10).draw(Palette::Orange);
}
}
# include <Siv3D.hpp>
void Main()
{
TCPServer server;
server.startAccept(50000);
Window::SetTitle(L"TCPServer: 接続待機中...");
Point p5Pos(0, 0);
while (System::Update())
{
if (server.isConnected())
{
Window::SetTitle(L"TCPServer: 接続完了!");
server.send(Mouse::Pos());
while (server.read(p5Pos));
}
if (server.hasError())
{
server.disconnect();
Window::SetTitle(L"TCPServer: 再接続待機中...");
server.startAccept(5500);
}
Circle(Mouse::Pos(), 30).draw(Palette::Skyblue);
Circle(p5Pos, 10).draw(Palette::Orange);
}
}
import processing.net.*;
import java.nio.ByteBuffer;
Client client;
int x = 0, y = 0;
void WriteInt8(char value)
{
client.write(value);
}
void WriteInt16(short value)
{
client.write(ByteBuffer.allocate(2).putShort(Short.reverseBytes(value)).array());
}
void WriteInt32(int value)
{
client.write(ByteBuffer.allocate(4).putInt(Integer.reverseBytes(value)).array());
}
void WriteInt64(long value)
{
client.write(ByteBuffer.allocate(8).putLong(Long.reverseBytes(value)).array());
}
void WriteFloat(float value)
{
WriteInt32(Float.floatToRawIntBits(value));
}
void WriteDouble(double value)
{
WriteInt64(Double.doubleToRawLongBits(value));
}
char ReadInt8()
{
return client.readChar();
}
short ReadInt16()
{
return Short.reverseBytes(ByteBuffer.wrap(client.readBytes(2)).getShort());
}
int ReadInt32()
{
return Integer.reverseBytes(ByteBuffer.wrap(client.readBytes(4)).getInt());
}
long ReadInt64()
{
return Long.reverseBytes(ByteBuffer.wrap(client.readBytes(8)).getLong());
}
float ReadFloat()
{
return Float.intBitsToFloat(ReadInt32());
}
double ReadDouble()
{
return Double.longBitsToDouble(ReadInt64());
}
void setup()
{
size(640, 480);
noStroke();
client = new Client(this, "127.0.0.1", 50000);
}
void draw()
{
background(0);
WriteInt32((int)mouseX);
WriteInt32((int)mouseY);
if(client.available() >= 8)
{
x = ReadInt32();
y = ReadInt32();
}
ellipse(mouseX, mouseY, 40, 40);
ellipse(x, y, 40, 40);
}
標準入出力を行うコンソールアプリケーションと通信ができます。PipeServer
から起動したアプリケーションは通常は非表示で実行されます。
# include <Siv3D.hpp>
/* [クライアント]
# include <iostream>
# include <string>
# include <sstream>
int main()
{
std::string input;
while (std::getline(std::cin, input))
{
if (input == "quit")
{
return 0;
}
int a, b;
std::stringstream{ input } >> a >> b;
std::cout << a * b << '\n';
}
std::cout << "error\n";
}
*/
void Main()
{
// クライアントのコンソールアプリケーションを選択
const auto path = Dialog::GetOpen({ { L"実行ファイル (*.exe)", L"*.exe" } });
if (!path)
{
return;
}
// クライアントを起動
PipeServer server(path.value());
if (!server)
{
return;
}
//
///////////////////////////////////
while (System::Update())
{
if (Input::KeyA.clicked)
{
Print(L"10*2=");
server.write("10 2\n");
}
if (Input::KeyB.clicked)
{
Print(L"3*4=");
server.write("3 4\n");
}
if (server.available())
{
std::string str;
server.read(str);
Print(Widen(str));
}
}
server.write("quit\n");
}