Here is the code for my C# server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
// Console.ReadLine();
while (true) {
String textinput;
textinput = Console.ReadLine ();
NetworkStream serverStream = clientSocket.GetStream ();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes (textinput + "$");
serverStream.Write (outStream, 0, outStream.Length);
serverStream.Flush ();
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
}
}
Then here is the code for c# client:
using System;
using System.Net.Sockets;
using System.Text;
namespace WindowsApplication1
{
class Form1
{
static void Main(string[] args)
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("10.132.198.29", 8888);
Console.WriteLine ("Running the client");
while ((true))
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)bytesFrom.Length);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from Server - " + dataFromClient);
string serverResponse = "Last Message from Server" + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// byte[] inStream = new byte[10025];
// serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
}
}
}
These all works fine in the terminal, but I want to convert the client into the unity project. So I create an empty object in my Unity project, and add the following code to the script, (made the few changes for the original c# client code).
using UnityEngine;
using System.Collections;
using System;
using System.Net.Sockets;
using System.Text;
public class Client : MonoBehaviour {
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
// Use this for initialization
void Start () {
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("10.132.198.29", 8888);
Debug.Log ("Running the client");
}
// Update is called once per frame
void Update () {
try
{
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)bytesFrom.Length);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Debug.Log(" >> Data from Server - " + dataFromClient);
string serverResponse = "Last Message from Server" + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Debug.Log(" >> " + serverResponse);
}
catch (Exception ex)
{
Debug.Log("Exception error:"+ex.ToString());
}
}
}
Actually, when I run my unity project. I can see the server print "Accept connection from client". When I change my ip, the unity project will stuck..So I think my unity project have successfully connected to the server. But in the unity project, the console print
Exception error:System.IO.IOException: Not connected
at System.Net.Sockets.NetworkStream..ctor (System.Net.Sockets.Socket socket, FileAccess access, Boolean owns_socket) [0x00000] in <filename unknown>:0
at System.Net.Sockets.NetworkStream..ctor (System.Net.Sockets.Socket socket, Boolean owns_socket) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Net.Sockets.NetworkStream:.ctor (System.Net.Sockets.Socket,bool)
at System.Net.Sockets.TcpClient.GetStream () [0x00000] in <filename unknown>:0
at Client.Update () [0x00000] in /Users/User/Unity3D/ClientTest/Assets/Client.cs:23
UnityEngine.Debug:Log(Object)
Client:Update() (at Assets/Client.cs:37)
When I type something in the server's terminal, it cannot print anything on the unity project console.. Can anyone help... Quite confused for a long time...