# Processing Network Client.available();

**URL:** <https://discourse.processing.org/t/processing-network-client-available/21886>\
**Category:** Libraries\
**Created:** [June 15, 2020, 10:09am UTC](https://discourse.processing.org/t/processing-network-client-available/21886 "2020-06-15T10:09:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moritz](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Moritz](https://discourse.processing.org/u/Moritz)\
**Post date:** [June 15, 2020, 10:09am UTC](https://discourse.processing.org/t/processing-network-client-available/21886/1 "2020-06-15T10:09:55Z")

</div>

Hi Guys,  
I work with the processing library and would like to know if there are any servers before I write. The Problem: client.available doesn’t work before writing:

Server:

```auto
import processing.net.*;
Server myServer;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, 5204); 
}

void draw() {
  Client c = myServer.available();
  if(c != null)
  background(c.read());
}

```

Client:

```auto
import processing.net.*; 

Client myClient; 
int dataIn; 
 int val = 0;
void setup() { 
  size(200, 200); 
  // Connect to the local machine at port 5204.
  // This example will not run if you haven't
  // previously started a server on this port.
  myClient = new Client(this, "127.0.0.1", 5204); 
} 
 
void draw() { 
  val = (val + 1) % 255;

  print(myClient.available());
  myClient.write(val);
  background(val); 
}

```

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [June 15, 2020, 12:36pm UTC](https://discourse.processing.org/t/processing-network-client-available/21886/2 "2020-06-15T12:36:17Z")

</div>

Hi @Moritz ,

I believe that the problem is on your Client IP code:  
` myClient = new Client(this, "127.0.0.1", 5204);`  
Check your computer IP to match the network you are using or search **what is my ip** on Google and try substituing there.

Also, for future research you might find helpful the following links:

> **[A Guide to Java Sockets | Baeldung](https://www.baeldung.com/a-guide-to-java-sockets)**
>
> This tutorial introduces Java sockets programming over TCP/IP with an actual Client/Server application.

  

> **[A Guide To UDP In Java | Baeldung](https://www.baeldung.com/udp-in-java)**
>
> A quick and practical guide to UDP in Java

  

> **[Read an InputStream using the Java Server Socket | Baeldung](https://www.baeldung.com/java-inputstream-server-socket)**
>
> Learn how to read data from a Java Server Socket.

  

> **[Guide to BufferedReader | Baeldung](https://www.baeldung.com/java-buffered-reader)**
>
> Learn the basics as well as some advanced features from the BufferedReader class

He provides a lot of information that I found useful myself.

Best regards

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 15, 2020, 12:44pm UTC](https://discourse.processing.org/t/processing-network-client-available/21886/3 "2020-06-15T12:44:32Z")

</div>

> [@MiguelSanches](#):
>
> Check your computer IP to match the network you are using or search **what is my ip** on Google and try substituing there.

Hi,

Actually you don’t need to know your global IP because `"127.0.0.1"` is referring to the [loopback adress](https://en.wikipedia.org/wiki/Localhost) (localhost) on your computer.

Your example works fine on my computer, the background is effectively changing so i didn’t understand what is the problem.

---

<div class="post-metadata">

**Author:** ![Moritz](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Moritz](https://discourse.processing.org/u/Moritz)\
**Post date:** [June 15, 2020, 1:19pm UTC](https://discourse.processing.org/t/processing-network-client-available/21886/4 "2020-06-15T13:19:38Z")

</div>

I want to know if a server is available or not before i write to it as a client. It works before loading but not before sending. If the client is receiving information from the server it works, but not if the client is sending information.

Example (cleint receives Information):

```auto

import processing.net.*;

Server myServer;
int val = 0;

void setup() {
  size(200, 200);
  // Starts a myServer on port 5204
  myServer = new Server(this, 5204); 
}

void draw() {
  val = (val + 1) % 255;
  background(val);
  myServer.write(val);
}

```

```auto

	

import processing.net.*; 

Client myClient; 
int dataIn; 
 
void setup() { 
  size(200, 200); 
  // Connect to the local machine at port 5204.
  // This example will not run if you haven't
  // previously started a server on this port.
  myClient = new Client(this, "127.0.0.1", 5204); 
} 
 
void draw() { 
  print(myClient.available());
  dataIn = myClient.read(); 
  background(dataIn); 
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 15, 2020, 3:08pm UTC](https://discourse.processing.org/t/processing-network-client-available/21886/5 "2020-06-15T15:08:28Z")

</div>

Hello,

> [@Moritz](#):
>
> I want to know if a server is available or not before i write to it as a client

I was able to integrate this with the server and client examples and it works:  
[https://discourse.processing.org/t/scan-for-servers/21814/4](https://discourse.processing.org/t/scan-for-servers/21814/4)

```auto
state = portIsOpen("127.0.0.1", 12345, 100); //returns a true or false that you can use

```

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 15, 2020, 5:13pm UTC](https://discourse.processing.org/t/processing-network-client-available/21886/6 "2020-06-15T17:13:56Z")

</div>

Another related post:  
[https://discourse.processing.org/t/sending-a-processing-array-from-one-computer-to-other-computers/20366/5](https://discourse.processing.org/t/sending-a-processing-array-from-one-computer-to-other-computers/20366/5)

It may be of interest to those using the network library.

`:)`

---

<div class="post-metadata">

**Author:** ![Moritz](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Moritz](https://discourse.processing.org/u/Moritz)\
**Post date:** [June 16, 2020, 9:57am UTC](https://discourse.processing.org/t/processing-network-client-available/21886/7 "2020-06-16T09:57:27Z")

</div>

I visited the Processing gitHub client page and found out how to easily see if the server is available.

> <https://github.com/processing/processing/blob/master/java/libraries/net/src/processing/net/Client.java>

`client.output != null`

Processing receives an outputStream from a socket, but the object can be null.

Thanks anyway
