Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 13, 2015 at 04:04 PM by RoCkHeArTeD for the following reason:

I solved the problem myself before anyone posted the answer here

avatar image
0
Question by RoCkHeArTeD · Sep 12, 2015 at 05:59 PM · c#javasocket

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;
                 }    
             }    
         }        
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Suddoha · Sep 12, 2015 at 06:46 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RoCkHeArTeD · Sep 13, 2015 at 04:05 PM 0
Share

@Suddoha ^

avatar image Suddoha RoCkHeArTeD · Sep 13, 2015 at 05:38 PM 0
Share

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges