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. :frowning:

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;
				}	
			}	
		}		
	}

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.