C# Tcp Connection to Unity Android Apk - c#

I have a tcp connection program. There are client and server sides. Client sides file is working on unity as script. Server sides file is working on main pc and as desktop application. When I open server and client files, they work on pc. If I also seperate this files to different pc's and again , they work. Up to here there is no any problem. However, if I want to build this client sides script file -which is on unity- as android apk and move it to my mobile device, client sides file is working but not connect to server. On computers, there is no any error, but apk file has error.
C# Server Side Code--
public Form1()
{
InitializeComponent();
}
//public virtual System.Windows.Forms.AnchorStyles Anchor { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine(string.Format("Starting TCP and UDP servers on port {0}...", 27015));
devices_battery_midlow_pic_1.Visible = false;
devices_battery_low_pic_1.Visible = false;
devices_battery_midhigh_pic_1.Visible = false;
devices_battery_high_pic_1.Visible = false;
play_button.Anchor = (AnchorStyles.Bottom);
Console.WriteLine(DateTime.Now.ToString() + " Getting IP...");
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPAddress ipAddress2 = ipHostInfo.AddressList[1];
connection_id_label.Text = "This IP:\n" + ipAddress2.MapToIPv4().ToString();
Console.WriteLine(DateTime.Now.ToString() + " Starting Connection Thread...");
connectionThread = new Thread(new ThreadStart(StartServer));
connectionThread.IsBackground = true;
connectionThread.Start();
}
private void StartServer()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
TcpListener listener = new TcpListener(IPAddress.Any, 27015);
listener.Start();
while (true)
{
using (TcpClient client = listener.AcceptTcpClient())
{
using (NetworkStream stream = client.GetStream())
{
byte[] buffer = new byte[client.ReceiveBufferSize];
int bytesRead = stream.Read(buffer, 0, client.ReceiveBufferSize);
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine($"Received: {dataReceived}");
//Thread staThread = new Thread(() => PasteText(dataReceived));
//staThread.SetApartmentState(ApartmentState.STA);
//staThread.Start();
}
}
}
}
Client Side C# Script--
void Start()
{
Debug.Log(string.Format("Starting TCP and UDP clients on port {0}...", 116));
//udpThread = new Thread(new ThreadStart(ClientThread));
//udpThread.Start();
ClientThread();
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 150, 100), debugString))
{
print("You clicked the button!");
}
}
void ClientThread()
{
using (TcpClient client = new TcpClient("10.10.10.73", 27015))
{
using (NetworkStream stream = client.GetStream())
{
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes("asdasd");
stream.Write(bytesToSend, 0, bytesToSend.Length);
}
}
}
It is too important for me. I'm waiting your help.

below is some connection demo from my other projects. It works on Android/iOS/Mac/PC without issue & tested. I used Loom for easy threading, you may grab it from somewhere github maybe.
or maybe just the way you type IP. It should be sth like this.
string IP = "127.0.0.1";
client.BeginConnect(IPAddress.Parse(IP), ClientListenPort, ClientEndConnect, null);
This is my working server listener:
//create listener
listener = new TcpListener(IPAddress.Any, ClientListenPort);
listener.Start();
listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Loom.RunAsync(() => {
while (!stop)
{
// Wait for client connection
clients.Add(listener.AcceptTcpClient());
clients[clients.Count - 1].NoDelay = true;
// We are connected
isConnected = true;
streams.Add(clients[clients.Count - 1].GetStream());
streams[streams.Count - 1].WriteTimeout = 500;
Loom.QueueOnMainThread(() => {
isConnected = true;
});
System.Threading.Thread.Sleep(1);
}
});
This is my working client demo connection:
Loom.RunAsync(() => {
// if using the IPAD
//client.Connect(IPAddress.Parse(IP), ClientListenPort);
client.BeginConnect(IPAddress.Parse(IP), ClientListenPort, ClientEndConnect, null);
while (!client.Connected)
{
System.Threading.Thread.Sleep(1);
}
//do sth...
});
void ClientEndConnect(IAsyncResult result)
{
client.EndConnect(result);
}

Related

TCPListener in windows form

I am trying to write a TCP listener that can connect to multiple clients and send and receive data.
Some of my code -
Calling server -
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cresijCamDataSet1.CentralControl' table. You can move, or remove it, as needed.
this.centralControlTableAdapter1.Fill(this.cresijCamDataSet1.CentralControl);
s = new Server();
Thread th = new Thread(s.Run);
th.Start();
}
Run Method -
public async void Run()
{
tcp = new TcpListener(IPAddress.Any, 1200);
tcp.Start();
while (true)
{
try
{
TcpClient client = await tcp.AcceptTcpClientAsync();
Thread th = new Thread(()=>{
Process(client);
}) ;
}
catch (Exception ex)
{
string m = ex.Message;
}
}
}
private async Task Process(TcpClient tcpClient)
{
bool hasItem = clients.Contains(tcpClient);
if(hasItem == false)
{
clients.Add(tcpClient);
}
IPEndPoint iPEndPoint =(IPEndPoint) tcpClient.Client.RemoteEndPoint;
string ip = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();
NetworkStream stream = tcpClient.GetStream();
byte[] receivedBytes = new byte[tcpClient.ReceiveBufferSize];
stream.Read(receivedBytes, 0, receivedBytes.Length);
f.UpdateData(receivedBytes, ip);
}
Sender Method to send data -
public void Sender(byte[] data, TcpClient client)
{
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
}
As you can see I have called Run() method in formLoad. But this all is not working as I dont have much knowledge of threads.
This method is not continuous. The server is not listening to clients always. Can someone help me with this. I need asynchronuous tcp listener that can listen to incoming clients and that too in windows form. Console Server I have.

How to receive data multiple times from a single client TCP c#

I am trying to listen for the client sending data multiple times. I do not know how to make the server listen to the client when the data is sent by the client a second time. For now i can send data by the client multiple times but the server will only accept the client data once. Please help.
Client
//the client
private TcpClient client = new TcpClient();
//constructor
public ClientConnect(string Ip, int Port)
{
IP = Ip;
PORT = Port;
}
//write the data to the server
public void Connect(byte[] clientId)
{
//try to connect to the server
Thread t = new Thread(() =>
{
while (true)
{
try
{
client.Connect(new IPEndPoint(IPAddress.Parse(IP), PORT));
break;
}
catch (SocketException)
{
Console.Clear();
Console.WriteLine("Trying to connect to the server...");
//pass if the connection failed
}
}
client.GetStream().Write(clientId, 0, (int)clientId.Length);
});
t.Start();
}
public void SendData(byte[] data)
{
Thread t2 = new Thread(() => {
while (true)
{
try
{
client.GetStream().Write(data, 0, (int)data.Length);
break;
}
catch (System.InvalidOperationException)
{
//pass
}
}
});
t2.Start();
}
Server:
while (true)
{
//Listen for a potential client
TcpClient client = tcpServer.AcceptTcpClient();
Console.WriteLine(client);
Console.WriteLine("Client connection accepted from " + client.Client.RemoteEndPoint + ".");
byte[] buffer = new byte[client.ReceiveBufferSize];
int bytesRead = client.GetStream().Read(buffer, 0, (int)buffer.Length);
byte[] buffer2 = new ArraySegment<byte>(buffer, 0, bytesRead).ToArray();
//do something with the data
}

'Connection refused' connecting through TCP/IP

I want to connect unity app on android with a computer application. I could run it on the network but over internet it is always saying the machine is refusing the connection. I have disabled the firewall of windows and anti-virus but still have problem connecting the server and Android client:
No connection could be made because the target machine actively refused it.
Here is the code:
Server side:
TcpListener tcpListener;
Socket socket;
NetworkStream networkStream;
Thread thread;
string getIpAddress()
{
System.Net.IPHostEntry host;
string localIp = "";
host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in host.AddressList)
{
localIp = ip.ToString();
}
return localIp;
}
public void ReceiveImage()
{
try
{
IPAddress localAddr = IPAddress.Parse("95.171.54.53");
tcpListener = new TcpListener(localAddr, 53100);
tcpListener.Start();
socket = tcpListener.AcceptSocket();
networkStream = new NetworkStream(socket);
pictureBox1.Image = Image.FromStream(networkStream);
if (socket.Connected == true)
{
while (true)
{
tcpListener.Stop();
ReceiveImage();
}
}
}
catch(Exception ex)
{
string Message = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(ReceiveImage));
thread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
tcpListener.Stop();
thread.Abort();
}
This is the client side:
// Update is called once per frame
int frameSaveCount = -1;
int FrameCount = -1;
Socket socket;
NetworkStream networkStream;
Thread thread;
MemoryStream memoryStream;
TcpClient tcpClient;
BinaryWriter binaryWriter;
void Update()
{
// ...
if(FrameCount % 10==0)
{
// ...
send(bytes);
}
}
void send(Byte[] bytes)
{
try
{
print(getIpAddress());
tcpClient = new TcpClient("95.171.54.53", 53100);
networkStream = tcpClient.GetStream();
binaryWriter = new BinaryWriter(networkStream);
binaryWriter.Write(bytes);
binaryWriter.Close();
networkStream.Close();
tcpClient.Close();
}
catch (Exception exception)
{
string message = exception.Message;
print(message);
}
}
the machine is refusing the connection
This error has exactly one meaning: nothing was listening at the IP:port you tried to connect to. So, either it was wrong, or your server wasn't started when your client tried to connect.
Image files have precisely nothing to do with it.

Simple socket server in Unity

I want to use a C# plugin in my Unity project. That plugin should act as a server which will get values from a client so that I'd be able to use those values for further processing.
The issue is that the server has infinite loop. And infinite loops cause Unity to hang. How to handle this?
EDIT: I'm attaching a code snippet of server program. In my opinion, there are 2 points which may be causing problem. The infinite loops and the point where program is suspended as commented in code:
void networkCode()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1755);
// Create a TCP/IP socket.
listener = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
// Program is suspended while waiting for an incoming connection.
Debug.Log("HELLO"); //It works
handler = listener.Accept();
Debug.Log("HELLO"); //It doesn't work
data = null;
// An incoming connection needs to be processed.
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
System.Threading.Thread.Sleep(1);
}
System.Threading.Thread.Sleep(1);
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
EDIT: After help from #Programmer, the C# plugin is complete. But Unity is not reading the correct values. I'm attaching the Unity side code:
using UnityEngine;
using System;
using SyncServerDLL; //That's our library
public class receiver : MonoBehaviour {
SynchronousSocketListener obj; //That's object to call server methods
// Use this for initialization
void Start() {
obj = new SynchronousSocketListener ();
obj.startServer ();
}
// Update is called once per frame
void Update() {
Debug.Log (obj.data);
}
}
I have tested SynchronousSocketListener class thoroughly in Visual Studio. It is giving good results there.
Use Thread to do your server Listen and read and write actions.
You can declare socket and other networkstream objects as public then initialize them in a thread function.
Unity does not work well with while loops in Threads and may freeze sometimes, but you can fix that by adding System.Threading.Thread.Sleep(1); in your while loop where you are reading or waiting for data to arrive from socket.
Make sure to stop the Thread in OnDisable() function. Do NOT access Unity API from the new Thread function. Just do only the socket stuff there and return the data to a public variable.
System.Threading.Thread SocketThread;
volatile bool keepReading = false;
// Use this for initialization
void Start()
{
Application.runInBackground = true;
startServer();
}
void startServer()
{
SocketThread = new System.Threading.Thread(networkCode);
SocketThread.IsBackground = true;
SocketThread.Start();
}
private string getIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
Socket listener;
Socket handler;
void networkCode()
{
string data;
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// host running the application.
Debug.Log("Ip " + getIPAddress().ToString());
IPAddress[] ipArray = Dns.GetHostAddresses(getIPAddress());
IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755);
// Create a TCP/IP socket.
listener = new Socket(ipArray[0].AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
keepReading = true;
// Program is suspended while waiting for an incoming connection.
Debug.Log("Waiting for Connection"); //It works
handler = listener.Accept();
Debug.Log("Client Connected"); //It doesn't work
data = null;
// An incoming connection needs to be processed.
while (keepReading)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
Debug.Log("Received from Server");
if (bytesRec <= 0)
{
keepReading = false;
handler.Disconnect(true);
break;
}
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
System.Threading.Thread.Sleep(1);
}
System.Threading.Thread.Sleep(1);
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
void stopServer()
{
keepReading = false;
//stop thread
if (SocketThread != null)
{
SocketThread.Abort();
}
if (handler != null && handler.Connected)
{
handler.Disconnect(false);
Debug.Log("Disconnected!");
}
}
void OnDisable()
{
stopServer();
}

Socket connection in CF 3.5 C#

I am trying to establish a communication between a handheld and a PC.
I have the following code, for the client:
public void connect(string IPAddress, int port)
{
// Connect to a remote device.
try
{
IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 1, 10 });
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
if (connectDone.WaitOne()){
//do something..
}
else{
MessageBox.Show("TIMEOUT on WaitOne");
}
}
catch(Exception e){
MessageBox.Show(e.Message);
}
}
My problem is that when I run both of them in a pc they communicate fine, but the same code in a SmartDevice Project doesn't connect with the Server which is running on the PC and it give me this error:
System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or stablished connection failed
because connected host has failed to respond
What am I missing?
NOTE: The IPAddress is hard coded inside the code
EDIT: here is another code which I take from a MSDN example. This don't work either, it says that it not possible to read. The server code in this case is the same as the example, the client code have a modification:
private void button1_Click(object sender, EventArgs e)
{
// In this code example, use a hard-coded
// IP address and message.
string serverIP = "192.168.1.10";//HERE IS THE DIFERENCE
string message = "Hello";
Connect(serverIP, message);
}
Thanks in advance for any help!
For my "mobile device" client, I send data to the "PC" host using this:
private void Send(string value) {
byte[] data = Encoding.ASCII.GetBytes(value);
try {
using (TcpClient client = new TcpClient(txtIPAddress.Text, 8000)) {
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
}
} catch (Exception err) {
// Log the error
}
}
For the host, you're best to use a thread or BackgroundWorker where you can let a TcpListener object sit and wait:
private void Worker_TcpListener(object sender, DoWorkEventArgs e) {
BackgroundWorker worker = (BackgroundWorker)sender;
do {
string eMsg = null;
int port = 8000;
try {
_listener = new TcpListener(IPAddress.Any, port);
_listener.Start();
TcpClient client = _listener.AcceptTcpClient(); // waits until data is avaiable
int MAX = client.ReceiveBufferSize;
NetworkStream stream = client.GetStream();
Byte[] buffer = new Byte[MAX];
int len = stream.Read(buffer, 0, MAX);
if (0 < len) {
string data = Encoding.UTF8.GetString(buffer);
worker.ReportProgress(len, data.Substring(0, len));
}
stream.Close();
client.Close();
} catch (Exception err) {
// Log your error
}
if (!String.IsNullOrEmpty(eMsg)) {
worker.ReportProgress(0, eMsg);
}
} while (!worker.CancellationPending);
}

Categories