mardi 25 mars 2014

socket programming writeUTF readUTf not working correctly


Vote count:

0




I am trying to implement an automatic repeat request technique using socket programming, but i am really new to socket programming. i have been stuck from the past couple of hours with the same error. any help would be appreciated.


Here is the server file.



import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
private ServerSocket serverSocket;
boolean running = true;
int totalP = 10;
int startP = 0;
int windowSize = 4;
int lastP = 3;
String[] storeP = new String[totalP];
boolean[] storeB = new boolean[totalP];
boolean[] nak = new boolean[totalP];
boolean[] ack = new boolean[totalP];

public GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(100000);
}

public void run()
{
while(true)
{
try
{
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
for (int i=0;i<totalP;i++)
{
ack[i]=false;
nak[i]=false;
storeB[i]=false;
}
/* DataInputStream in =
new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
*/

while(running)
{

String tempS = in.readUTF();
System.out.println("Just got this from client: "+tempS);
int tempI = Character.getNumericValue(tempS.charAt(2));
if(ack[tempI]==true) {
System.out.println("Packet already received! Duplicate Packet, Resending ACK");
out.writeUTF("ACK"+tempI);
out.flush();
}
else if((tempS.contains("COR")) && (nak[tempI]!=true)) {
out.writeUTF("NAK"+tempI);
out.flush();
nak[tempI]=true;
}
else if((!(tempI>=startP) && (tempI<=lastP)) && (nak[tempI]!=true)) {
out.writeUTF("NAK"+tempI);
out.flush();
nak[tempI]=true;
}
else if (((tempI>=startP) && (tempI<=lastP)) && (nak[tempI]!=true)) {
if (tempI>startP) {
storeP[tempI]=tempS;
storeB[tempI]=true;
System.out.println("Sending ACK for Packet: "+tempI);
ack[tempI]=true;
out.writeUTF("ACK"+tempI);
out.flush();
}
else if (tempI==startP)
{
out.writeUTF("ACK"+tempI);
out.flush();
System.out.println("Sending ACK for Packet: "+tempI);
ack[tempI]=true;
startP++;
lastP++;
for(int i=1;i<windowSize && storeB[(tempI+i)]==true;i++)
{
startP++;
lastP++;
}
}

}

System.out.println("startP value: "+startP+", lastP value: "+lastP);
}
if(!running) {
server.close();
}
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = Integer.parseInt(args[0]);
try
{
Thread t = new GreetingServer(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}


and here is the client file



// File Name GreetingClient.java

import java.net.*;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;

public class GreetingClient
{
static int resendP;
static boolean resendT = false;
public static void main(String [] args)
{

int tempcoun = 0;
int currentP = 0;
int windowSize = 4;
int totalP = 10;
int startP = 0;
int lastP = 3;
boolean running = true;
//String tempR;
boolean[] ack = new boolean[totalP];
Timer[] timer = new Timer[totalP];
for(int i=0;i<totalP;i++)
{
timer[i] = new Timer();
ack[i]=false;
}
//TimerTask task = new TimerTask;

class task extends TimerTask {

int tempcurrentP;

public task(int tempcurrentP) {
this.tempcurrentP = tempcurrentP;
}

public void run() {
resendP = tempcurrentP;
resendT = true;
}
}

String serverName = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());



System.out.println("Please enter a binary message less than 10 bis");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String data = br.readLine();
System.out.println("data is: "+data);
while(running)
{
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
String tempR;
tempcoun++;
if ((currentP < windowSize) && (currentP <= totalP))
{
System.out.println("char is "+ (data.charAt(currentP)) + ", position is " + currentP);
String temp = Character.toString(data.charAt(currentP));
//System.out.println(temp+" tempcoun value:"+tempcoun);
temp = temp + " " + currentP;
System.out.println("sending "+temp);
out.writeUTF(temp);
out.flush();
System.out.println("sent "+temp);
timer[currentP].schedule(new task(currentP), 0, 10000);
currentP++;
}
tempR = in.readUTF();
System.out.println("Received: "+tempR);
int tempRInt= Character.getNumericValue(tempR.charAt(3));
if (tempR.contains("ACK"))
{
ack[tempRInt]=true;
System.out.println("Received ACK for packet: "+tempRInt);
if(startP == tempRInt)
{
startP++;
lastP++;
System.out.println("startP value: "+startP+", lastP value: "+lastP);
while(ack[startP]==true)
{
startP++;
lastP++;
System.out.println("startP value: "+startP+", lastP value: "+lastP);
}
}
}
if (tempR.contains("NAK"))
{
int tempInt = Character.getNumericValue(tempR.charAt(3));
if ((tempInt >= startP) && (tempInt <= lastP)) {
timer[tempInt].cancel();
String temp = Character.toString(data.charAt(tempInt));
out.writeUTF(temp);
out.flush();
timer[tempInt].schedule(new task(currentP), 0, 10000);
}
}
if (tempR.contains("COR"))
{

}

if (resendT == true)
{
timer[resendP].cancel();
String temp = Character.toString(data.charAt(resendP));
out.writeUTF(temp);
out.flush();
timer[resendP] = new Timer();
timer[resendP].schedule(new task(resendP), 0, 10000);
}

in.close();
out.close();
}
/* out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
*/
if(!running) {
client.close();
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}


and here are the error's and output i am getting


server output



Waiting for client on port 6066...
Just connected to /127.0.0.1:22568
Just got this from client: 1 0
Sending ACK for Packet: 0
startP value: 1, lastP value: 4
Just got this from client: 1
Exception in thread "Thread-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(Unknown Source)
at GreetingServer.run(GreetingServer.java:56)


client output



Connecting to localhost on port 6066
Just connected to localhost/127.0.0.1:6066
Please enter a binary message less than 10 bis
1010101010
data is: 1010101010
char is 1, position is 0
sending 1 0
sent 1 0
Received: ACK0
Received ACK for packet: 0
startP value: 1, lastP value: 4
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Unknown Source)
at GreetingClient.main(GreetingClient.java:63)


the server is getting the wrong message the second time it reads from the client. i don't understand why that is happening! please help!




demo.b

870

asked 4 mins ago






Aucun commentaire:

Enregistrer un commentaire