- Home /
I solved the problem myself before anyone posted the answer here
I pressed a combination of shortcut keys, something happened..
I pressed CTRL + ALT + C in MonoDevelop to comment a block of code as it should but it didn't. Something else happened instead which was probably related to Network or Connection and just had an OK button in the notification dialogue box. I didn't pay attention to it that moment(yesterday) and now my script isn't behaving right.
My unity C# code communicates with a local Java server. Before this problem, I didn't have to play the app twice in Unity for the connection to establish, it happened in the first instance itself.
I'm also trying to send a text to my server on the click of a button, but the server wouldn't display it unless I don't stop and play the app again. I believe the key combination I pressed is the reason for such a behavior. What could it be and how can I fix it up? Please please please help me out. :(
Here in my C# code:-
using UnityEngine;
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
public class ServerConnector : MonoBehaviour {
static Int32 port2= 4444;
static TcpClient client;
public static NetworkStream stream;
void Awake()
{
DontDestroyOnLoad (this.gameObject);
try
{
client= new TcpClient("192.168.1.9", port2);
stream = client.GetStream();
Debug.Log("Checkpoint");
Byte[][] data= new byte[2][];
data[0]= System.Text.Encoding.UTF8.GetBytes("This is a message from c# script");
data[1]=System.Text.Encoding.UTF8.GetBytes("String 2 from c# script");
Debug.Log("Checkpoint 2");
// Send the message to the connected TcpServer.
stream.Write(data[0], 0, data[0].Length);
stream.Write(data[1], 0, data[1].Length);
Debug.Log("Checkpoint 3");
}
catch (Exception ex)
{
Debug.Log("This is an exception: " + ex.ToString());
}
}
}
}
Here is the code for onClick()
using UnityEngine;
using System;
using System.Collections;
public class EventListeners : MonoBehaviour {
public void SignUp_bOnClick () {
try
{
System.Byte[][] data= new byte[1][];
data[0]= System.Text.Encoding.UTF8.GetBytes("void SignUp_bOnClick () called");
ServerConnector.stream.Write(data[0], 0, data[0].Length);
Debug.Log ("Mouse on click fired");
}
catch (Exception ex)
{
Debug.Log("This is an exception: " + ex.ToString());
}
}
}
Here is my Java server code. I've used threading to handle multiple user connections. I'm only going to post the code of the class that extends thread because the code of other class which creates each thread isn't needed here.
PS- This code isn't properly done regarding NullPointerException but I don't need to worry about it till I don't fix this problem.
import java.io.*;
import java.net.*;
public class CsServer extends Thread {
private Socket socket;
public CsServer(Socket clientSocket)
{
this.socket = clientSocket;
//System.out.println("Just connected to " + server.getRemoteSocketAddress());
//serverSocket.setSoTimeout(60*10000);
}
public void run(){
boolean i=true;
while (i) {
try {
// System.out.println("Checkpoint 1");
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//DataInputStream inFromServer = new
// System.out.println("Checkpoint 2");
while (i) {
String response= new String(inFromServer.readLine());
if(response!= null){
System.out.println("Checkpoint 3");
System.out.println(response);
System.out.println("Checkpoint 4");
}
}
System.out.println("Closing connection");
socket.close();
inFromServer.close();
} catch (IOException e) {
System.out.println("I/O error: ");
e.printStackTrace();
i= false;
}
catch(NullPointerException n){
System.out.println("NULL error: ");
n.printStackTrace();
i= false;
}
}
}
}
Are any of the Debug.Logs or System.outs printed? That would help alot to get a bit closer to the problem. Also, one thread per client is probably not a good idea.
Answer by RoCkHeArTeD · Sep 13, 2015 at 04:03 PM
Sorry that I forgot to update the thread, the problem was with server. I shouldn't be using BufferedReader as it looks for some number of extra characters while reading. Thank you for your time to reply my query though. If you want, I can post the code on how I solved the problem.
Don't worry, it's alright. I'm glad you solved the issue. I also write a server-sided application, kind of backend for my current project with SSL/TLS encryption, but it's completely in C#. But you can post it for others who might have troubles similar to this one, but only if you want to. ;)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Data keeps "hanging". TCP client/server 2 Answers
Basic C# Public Variable Help 3 Answers