Socket Low Transfer Speed - Win7 .Net4 - CentOSx64 Mono 2.10 - sockets

I have created a simple client/server app and it works great (1MB/s which is max speed i set for server to send) when i run in locally or under Lan network. But when i try to run it in one of my Dedicate Server/VPS my download speed become slow.
I am using CentOS on servers and Mono for running it, Mono version is 2.10.2 and CentOs is 64bit version. Created using framework 4.
Speed test:
Local: 1MB
Lan: 1MB
Running server on CentOS: 0~10~20 KB
My connection speed is 2Mb or ~250KB. It will give me full speed some times. But very rare and i cant see why it give my full speed sometimes and sometimes no speed at all or why sometimes only 10KB and other times only 20KB. Also, i am running client part on my Win7 Desktop. Here is code for server and client part:
Server:
class Program
{
private static BackgroundWorker _ListeningWorker;
private static BackgroundWorker _QWorker;
private static System.Net.Sockets.Socket _Server;
private static List<System.Net.Sockets.Socket> ConnectedClients = new List<System.Net.Sockets.Socket>();
static void Main(string[] args)
{
Program._ListeningWorker = new BackgroundWorker();
Program._ListeningWorker.WorkerSupportsCancellation = true;
Program._ListeningWorker.DoWork += _ListeningWorker_DoWork;
Program._QWorker = new BackgroundWorker();
Program._QWorker.WorkerSupportsCancellation = true;
Program._QWorker.DoWork += _QWorker_DoWork;
Program.Start();
while (true)
{
Console.Clear();
Console.WriteLine("1.0.0.1");
Console.WriteLine(Program.ConnectedClients.Count.ToString());
System.Threading.Thread.Sleep(1000);
}
}
public static bool Start()
{
if (!Program._ListeningWorker.IsBusy)
{
Program._Server = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8081);
Program._Server.Bind(ipLocal);
Program._Server.Listen(10);
Program._ListeningWorker.RunWorkerAsync();
Program._QWorker.RunWorkerAsync();
}
return true;
}
private static void _ListeningWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (!Program._ListeningWorker.CancellationPending)
{
if (Program._Server.Poll(10, SelectMode.SelectRead))
{
lock (ConnectedClients)
{
Program.ConnectedClients.Add(Program._Server.Accept());
}
}
System.Threading.Thread.Sleep(1);
}
Program._Server.Close();
}
private static void _QWorker_DoWork(object sender, DoWorkEventArgs e)
{
byte[] array = new byte[1024];
Random random = new Random();
while (!Program._QWorker.CancellationPending)
{
if (ConnectedClients.Count > 0)
{
System.Net.Sockets.Socket[] st;
lock (ConnectedClients)
{
st = new System.Net.Sockets.Socket[Program.ConnectedClients.Count];
ConnectedClients.CopyTo(st);
}
foreach (System.Net.Sockets.Socket ser in st)
{
random.NextBytes(array);
try
{
ser.BeginSend(array, 0, array.Length, SocketFlags.None, (AsyncCallback)delegate(IAsyncResult ar)
{
try
{
ser.EndSend(ar);
}
catch (Exception)
{
iDissconnected(ser);
}
}, null);
}
catch (Exception)
{
iDissconnected(ser);
}
}
}
System.Threading.Thread.Sleep(1);
}
Program._Server.Close();
}
internal static void iDissconnected(System.Net.Sockets.Socket client)
{
lock (ConnectedClients)
for (int i = 0; i < ConnectedClients.Count; i++)
if (ConnectedClients[i].Equals(client))
{
ConnectedClients.RemoveAt(i);
i--;
}
}
}
Client:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter IP Address: ");
string Address = Console.ReadLine();
Console.WriteLine();
ushort Port = 8081;
System.Net.Sockets.Socket Client;
Client = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Client.Connect(Address, (int)Port);
Console.WriteLine("Connected");
int p = 0;
int t = Environment.TickCount;
while (true)
{
if (Client.Available > 0)
{
byte[] z = new byte[1024];
int r = Client.Receive(z); ;
p += r;
}
else
{
System.Threading.Thread.Sleep(10);
}
if (Environment.TickCount - t >= 1000)
{
t = Environment.TickCount;
Console.WriteLine(Program.FormatFileSizeAsString(p) + " Readed,");
p = 0;
}
}
}
public static string FormatFileSizeAsString(int len)
{
if (len < 750 && len > 0)
return "0.0 B ~";
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = len;
for (i = 0; (int)(len / 1024) > 0; i++, len /= 1024)
dblSByte = len / 1024.0;
return String.Format("{0:0.0} {1}", dblSByte, Suffix[i]);
}
}
Thanks all, Please tell me what you think about possible problems.

It was some sort of limitation by ISP per each connection.
Solving was funny. I used to send a HTTP Header before my request and ISP increased my speed because of that.

Related

Multi-Client Chat and file transfer in linux

\I want to make a socket application that will run on Linux and provide multiple messaging and file transfer. Can you share sample code? Can you help me i am a second year computer engineering student?
I found a similar code in my research, but I cannot connect to my linux server from different computers.I learned this code from github #HondaPL.
This is client codes
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using MessengerLogic;
using System.IO;
namespace Messenger
{
public class Connection
{
public static int BufferSize = 1024;
public static string nick;
public static IPAddress ip = IPAddress.Parse("-.-.-.-");
public static int port = 1234;
public static TcpClient client = new TcpClient();
public static List<string> users = new List<string>();
public static bool isEnd = false;
public static string[] fromServer = new string[30];
public void Connect(string nickname)
{
nick = nickname;
client.Connect(ip, port);
nickname += "$Welcome$";
byte[] buffer = Encoding.ASCII.GetBytes(nickname);
NetworkStream ns = client.GetStream();
Console.WriteLine(nickname);
ns.Write(buffer, 0, buffer.Length);
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
}
static void ReceiveData(TcpClient client)
{
NetworkStream ns = client.GetStream();
byte[] receivedBytes = new byte[1024];
int byte_count;
StringParser parser = new StringParser();
while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
{
fromServer = new string[30];
users.Clear();
string message = Encoding.ASCII.GetString(receivedBytes, 0, byte_count);
fromServer = parser.Parser(message);
if (fromServer[0] == "List")
{
for (int i = 1; i < fromServer.Length; i++)
{
users.Add(fromServer[i]);
}
isEnd = true;
}
else
{
Console.WriteLine(message);
Console.WriteLine(fromServer[0]);
Console.WriteLine(fromServer[1]);
FrmOnlineUsers.frm.AssignValues(fromServer[0], fromServer[1]);
FrmOnlineUsers.frm.MessageFromClient();
}
}
}
public List<string> Refresh()
{
NetworkStream ns = client.GetStream();
string temp = nick;
temp += "$List$";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
while (!isEnd)
{
}
isEnd = false;
return users;
}
public void SendMessage(string who, string msg)
{
NetworkStream ns = client.GetStream();
string temp = $"{who}$Message${msg}$";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
}
public void FileSend(string filepath, string nick, string fileName)
{
NetworkStream ns = client.GetStream();
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] bytesToSend = new byte[fs.Length];
int numBytesRead = fs.Read(bytesToSend, 0, bytesToSend.Length);
int totalBytes = 0;
//Tutaj trzeba dodać, aby pobierał nazwę pliku
string temp = $"{nick}$File${fileName}${fs.Length}";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
Thread.Sleep(1000);
for (int i = 0; i <= fs.Length / BufferSize; i++)
{
if (fs.Length - (i * BufferSize) > BufferSize)
{
ns.Write(bytesToSend, i * BufferSize,
BufferSize);
totalBytes += BufferSize;
}
else
{
ns.Write(bytesToSend, i * BufferSize,
(int)fs.Length - (i * BufferSize));
totalBytes += (int)fs.Length - (i * BufferSize);
}
}
fs.Close();
}
}
}

Both client and server windows are blank

I am trying to test player position communication between a client and a server. I am doing this by building a game environment where the other player's position is also displayed. They connect properly, but both the windows are blank white. If I terminate one program only then the other displays the playground. Also in the action listener which listens for the timer only sends the data and doesn't proceed. Can I get help for displaying the windows properly and fixing the action listener?
Here is the client code :
public class SamplePlayerClient1
{
private static JFrame window = new JFrame("Sample Player Client 1");
private static class Ground extends JPanel {
private static final long serialVersionUID = 1L;
private int px = 50, py = 235, ex, ey, key;
private DataWriter writer;
private DataReader reader;
private ActionListener timerListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
writer.send(px, py);
System.out.println("data sent");
ex = reader.read().x;
ey = reader.read().y;
System.out.println("Data read");
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
};
private Timer animator = new Timer(30, timerListener);
private KeyAdapter keyListener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
py -= 8;
if(py < 0) {
py = 0;
}
} else if(key == KeyEvent.VK_DOWN) {
py += 8;
if(py > 430) {
py = 430;
}
} else if(key == KeyEvent.VK_LEFT) {
px -= 8;
if(px < 0) {
px = 0;
}
} else if(key == KeyEvent.VK_RIGHT) {
px += 8;
if(px > 455) {
px = 455;
}
}
}
};
Ground() throws UnknownHostException, IOException {
Socket soc = new Socket(InetAddress.getByName("192.168.0.3"), 9998);
System.out.println("client 2 connected");
writer = new DataWriter(soc);
reader = new DataReader(soc);
animator.start();
addKeyListener(keyListener);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
requestFocus();
g.setColor(Color.BLACK);
g.fillRect(px, py, 30, 30);
g.setColor(Color.RED);
g.fillRect(ex, ey, 30, 30);
}
}
private static class DataWriter {
private OutputStreamWriter writer;
private String point = "";
DataWriter(Socket soc) throws IOException {
writer = new OutputStreamWriter(soc.getOutputStream());
}
void send(int x, int y) throws IOException {
point = "" + x;
point += ",";
point += y;
point += "\n";
writer.write(point);
writer.flush();
}
}
private static class DataReader {
private InputStreamReader is;
private BufferedReader reader;
private String point = "";
DataReader(Socket soc) throws IOException {
is = new InputStreamReader(soc.getInputStream());
reader = new BufferedReader(is);
}
Point read() throws IOException {
point = reader.readLine();
int x = Integer.parseInt(point.split(",")[0]);
int y = Integer.parseInt(point.split(",")[1]);
return new Point(x, y);
}
}
public static void main(String [] args) throws UnknownHostException, IOException {
Ground ground = new Ground();
window.setContentPane(ground);
window.setSize(600, 600);
window.setLocation(0, 0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
I don't see the problem but I did see that:
ex = reader.read().x;
ey = reader.read().y;
This reads two lines and throws away the y from the first and the x from the second.
Should be:
Point p = reader.read();
ex = p.x;
ey = p.y;
The peer might not generate a point for you to read every time you write one so the reading of the points from the peer should not occur in the event handler that writes a point to the peer.

Unity TCP server freezing

I'm trying to get tcp server working in unity, and it's kinda working. My systems purpose is to read data from another program that sends me bytes and draws me a picture of that data. Main problem is that when I run it, it works for a while (random time) and then whole unity freezes and I need to kill it from the task manager.
void Start()
{
Display1 = gameObject.GetComponent<Renderer>();
mymat = GetComponent<Renderer>().material;
packetReady = false;
tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests));
tcpListenerThread.IsBackground = true;
tcpListenerThread.Start();
bytes = new byte[1024];
tex = new Texture2D(800, 1280, TextureFormat.RGB24, false);
firstTime = true;
ArrayInit(3072060);
packetLength = 3072060;
myThread = new Thread(Draw);
myThread.Start();
}
void Draw()
{
if (Loader == null)
{
return;
}
else if (packetReady)
{
tex.LoadRawTextureData(Loader);
tex.Apply();
mymat.SetTexture("_EmissionMap", tex);
Display1.material.mainTexture = tex;
packetReady = false;
}
}
void Update()
{
Draw();
}
private void ListenForIncommingRequests()
{
try
{
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 35800);
tcpListener.Start();
while (true)
{
if (!tcpListener.Pending())
{
Thread.Sleep(100);
}
Thread.Sleep(10);
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
if (bytes == null)
{
return;
}
else
{
ParseData(bytes);
}
}
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
finally
{
tcpListener.Stop();
}
}
I have no idea what is causing this, I've tried to solve the problem but nothing seems to work. Any suggestions?

WebGL and UNet. Cant seem to get in proper contact with server from a browser

I am trying to setup a multiplayer game to be played through a browser using UNet. In the editor everything works fine, I followed a tutorial (https://www.youtube.com/watch?v=qGkkaNkq8co) that said it should work in webgl but sadly it does not. There are two scripts as of yet one for the client and one for the server linked below. This is using mostly the LLAPI.
Whether I use the networkServer.usewebsocket = true or not, the result is the same.
The debug of "finished connection" does show so it completes the OnConnection function.
From the editor everything works fine but from the browser I get the following error:
"Attempt to send to not connected connection {1}" -- Since I get this error when ASKNAME is asked and when ASKPOSITION is asked I suspect that the SEND function is the problem im trying to send something the browser cant understand. But I don't know why.
private int port = 3001;
private List<ServerClient> clients = new List<ServerClient>();
private float lastMovementUpdate;
private float movementUpdateRate = 0.05f;
private int socketId;
private int webHostId;
private int reliableChannel;
private int unReliableChannel;
private bool isStarted = false;
private byte error;
private void Start()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unReliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
socketId = NetworkTransport.AddHost(topo, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topo, port, null);
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.Nothing: //1
break;
case NetworkEventType.ConnectEvent: //2
Debug.Log("Player " + connectionId + "has connected");
OnConnection(connectionId);
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving From " + connectionId + "has sent : " + msg);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "NAMEIS":
OnNameIS(connectionId, splitData[1]);
break;
case "MYPOSITION":
OnMyPosition(connectionId, float.Parse(splitData[1]), float.Parse(splitData[2]));
break;
default:
Debug.Log("invalid message : " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent: //4
Debug.Log("Player " + connectionId + "has disconnected");
OnDiconnection(connectionId);
break;
}
if (clients.Count > 0)
{
if (Time.time - lastMovementUpdate > movementUpdateRate)
{
lastMovementUpdate = Time.time;
string posMsg = "ASKPOSITION|";
foreach (ServerClient sc in clients)
posMsg += sc.connectionId.ToString() + '%' + sc.position.x.ToString() + '%' + sc.position.y.ToString() + '|';
posMsg = posMsg.Trim('|');
Send(posMsg, unReliableChannel, clients);
}
}
}
private void OnConnection(int cnnId)
{
Debug.Log("Arrived at connection");
ServerClient c = new ServerClient();
c.connectionId = cnnId;
c.playerName = "TEMP";
clients.Add(c);
string msg = "ASKNAME|" + cnnId + "|";
foreach (ServerClient sc in clients)
msg += sc.playerName + '%' + sc.connectionId + '|';
msg = msg.Trim('|');
Send(msg, reliableChannel, cnnId);
Debug.Log("Finished at connection");
}
private void OnNameIS(int cnnId, string playerName)
{
// link the name to the connection ID
clients.Find(x => x.connectionId == cnnId).playerName = playerName;
// Tell evertone that a new player has connected
Send("CNN|" + playerName + '|' + cnnId, reliableChannel, clients);
}
private void Send(string message, int channelId, int cnnId)
{
List<ServerClient> c = new List<ServerClient>();
c.Add(clients.Find(x => x.connectionId == cnnId));
Send(message, channelId, c);
}
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(socketId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
private void OnDiconnection(int cnnId)
{
clients.Remove(clients.Find(x => x.connectionId == cnnId));
string msg = "DC|" + cnnId;
Send(msg, reliableChannel, clients);
}
private void OnMyPosition(int cnnId, float x, float y)
{
clients.Find(clientInQuestion => clientInQuestion.connectionId == cnnId).position = new Vector3(x, y, 0);
}
And then the client:
private const int MAX_CONNECTION = 100;
private int port = 3001;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unReliableChannel;
private int connectionId;
private int clientId;
private bool isConnected = false;
public bool isStarted = false;
private float connectionTime;
private string playerName;
private byte error;
public GameObject playerPrefab;
public Dictionary<int, player> players = new Dictionary<int, player>();
public void Connect()
{
Debug.Log("connecting...");
// does player have a name?
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "")
{
Debug.Log("Enter a name");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unReliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost(topo, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
Debug.Log("connected");
}
private void Update()
{
connectionTime += Time.deltaTime;
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("receiving : " + msg);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "ASKNAME":
OnAskName(splitData);
break;
case "CNN":
SpawnPlayer(splitData[1], int.Parse(splitData[2]));
break;
case "DC":
playerDisconnected(int.Parse(splitData[1]));
break;
case "ASKPOSITION":
OnAskPosition(splitData);
break;
default:
Debug.Log("invalid message : " + msg);
break;
}
break;
}
}
private void OnAskName(string[] data)
{
// set the client ID
clientId = int.Parse(data[1]);
// send our name to the server
Send("NAMEIS|" + playerName, reliableChannel);
// create all the other players
for (int i = 2; i < data.Length - 1; i++)
{
string[] d = data[i].Split('%');
SpawnPlayer(d[0], int.Parse(d[1]));
}
}
private void OnAskPosition(string[] data)
{
if (!isStarted)
return;
//update everyone else
for (int i = 1; i <= data.Length-1; i++)
{
string[] d = data[i].Split('%');
//prevent the server from updating us
if (clientId != int.Parse(d[0]))
{
Debug.Log("updating position");
Vector3 position = Vector3.zero;
position.x = float.Parse(d[1]);
position.y = float.Parse(d[2]);
players[int.Parse(d[0])].avatar.transform.position = position;
}
}
//send out own position
Vector3 myPosition = players[clientId].avatar.transform.position;
string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString();
Send(m, unReliableChannel);
}
private void SpawnPlayer(string playerName, int cnnId)
{
GameObject go = Instantiate(playerPrefab) as GameObject;
// is this ours?
if(cnnId == clientId)
{
// add mobility
go.AddComponent<PlayerController>();
// remove Canvas
GameObject.Find("Canvas").SetActive(false);
isStarted = true;
}
player p = new player();
p.avatar = go;
p.playerName = playerName;
p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
p.connectionId = cnnId;
players.Add(cnnId, p);
}
private void Send(string message, int channelId)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
NetworkTransport.Send(hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
private void playerDisconnected(int cnnId)
{
Destroy(players[cnnId].avatar);
Debug.Log("player : " + players[cnnId].playerName + " has disconnected");
players.Remove(cnnId);
}
}
Any feedback would be greatly appreciated, I have tried looking through the documentation but I feel I have done everything that it says to do in that (https://docs.unity3d.com/Manual/UNetUsingTransport.html) at the bottom it talks about webgl support, but it doesn't seem to have worked.
Fixed this issue,
Answer was that I was sending to HostID and not webSocketId
so this:
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(hostId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
was changed to this:
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(webSocketId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}

Arduino Ethernet Shield connection to socket server

I'm using an ethernet shield for Arduino to connect it to a socket server (different computer) so that I can receive messages from it to activate some routine. Here is my code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x21 };
byte ip[] = { 192,168,1,11 }; //ip shield
byte server[] = { 192,168,1,7 }; // ip server
EthernetClient client;
String readString;
int ledPins[] = {19, 17, 2,3, 5, 6, 7, 8, 9}; // leds pins
int pinCount = 8;// number of leds
int const PINEYES = 9; //pin for different led
int const TIMERLEDS = 1000;
int const TIMERTOOFF= 3000;
//--------------------------------------------------------------------------
void setup() {
turnOffLeds();
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 1400)) {
Serial.println("connected");
client.println();
} else {
Serial.println("connection failed");
}
pinMode(PINEYES, OUTPUT);
int thisPin;
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
//--------------------------------------------------------------------------
void loop() {
if (client.available()) {
char c = client.read();
if (readString.length() < 30) {
if(c!='|')
readString.concat(c);
else {
client.flush();
//if (readString == "START_SENSATIONS") {
if (readString == "on") {
Serial.println("recebi");
client.stop();
turnOnMaya();
}
resetString();
}
}
Serial.println(readString);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
//--------------------------------------------------------------------------
void turnOnMaya(){
turnOnLeds();
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
delay(TIMERLEDS);
digitalWrite(ledPins[thisPin], LOW);
}
turnOnEyes();
delay(TIMERTOOFF);
turnOffLeds();
digitalWrite(PINEYES, LOW);
client.connect(server, 1400);
}
//--------------------------------------------------------------------------
void turnOnLeds(){
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
}
}
//--------------------------------------------------------------------------
void turnOffLeds(){
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], LOW);
}
}
//--------------------------------------------------------------------------
void turnOnEyes(){
digitalWrite(PINEYES, 255);
}
//--------------------------------------------------------------------------
void resetString() {
readString = "";
}
The problem is, when my server stops or is not available for some moments, I need my Arduino to keep on trying to connect to it until it is available again. But I can't make this work.
I tried this:
while(!client.available()){
Serial.println("connection failed, trying again...");
client.connect(server, 1400);
delay(1000);
}
But it doesn't work. It just prints "connection failed, trying again..." forever. How can I do this?
Thanks!
I'm assuming that the server in your PC is a normal java or c (or any other standard tcp server)
But your arduino client doesn't specify that it is TCP. So either change your server or the client(like in here - this uses wifi connection).
If your server is in java, it could be like this:
int port=9999;
try{
System.out.println("Starting server...");
ServerSocket ss=new ServerSocket(port);
Socket clientSocket=ss.accept();
System.out.println("Connection has been established...");
PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
System.out.println("Listening.....");
while((inputLine=br.readLine())!=null)
System.out.println(inputLine);
}catch(Exception e){System.out.println(e.getMessage());}