Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Client Server TCP/IP Communication NOT working | Socket Programming

911989Jan 19 2012 — edited Jan 19 2012
Hi All,

The connections are binding however its not sending the data. Let me know if i have missed something ?

Thanks,
Kumar

-----------------------------------------------X-----------------------------------------------------------------X--------------
//Client Class
package com.crude.transport;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientSocketCommunication
{
Socket skt;
OutputStreamWriter sw;

public void prepareSocket()
{
try
{
skt = new Socket("NBMUMNISHANT.apac.progress.com", 8888);
}catch (Exception e)
{
System.out.println("Error while creating socket for client");
e.printStackTrace();
}
}
public void sendData()
{
if (skt.isBound())
{
System.out.println("Client Socket binded properly");
try
{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
//sw = new OutputStreamWriter(skt.getOutputStream());
bw.write("Data from the client side .adlklkasjdlkajsd");
System.out.println("Sent data to Server");
} catch (Exception e)
{
System.out.println("Error while sending data to Server");
}
}
else
{
System.out.println("Error while binding client socket");
}
}
public void closeSocket()
{
try
{
skt.close();
System.out.println("Closing client socket");
} catch (Exception e)
{
System.out.println("Error while Closing client socket");
}
}

}
-----------------------------------------------X-----------------------------------------------------------------X--------------

//Server Class

package com.crude.transport;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerSocketCommunication
{
ServerSocket socketHandler;
Socket skt = new Socket();

public void prepareServer()
{
try
{
socketHandler = new ServerSocket(8888);
try
{
System.out.println("Server is waiting at "
+ InetAddress.getLocalHost().getCanonicalHostName()+ ":"
+ socketHandler.getLocalPort());
} catch (Exception e)
{
System.out.println("Error while printing server add");
}

skt = socketHandler.accept();
System.out.println("Client logged in!!!!!!!!!!!!!!!");
}catch(Exception e)
{
System.out.println("Error while preparing socket for server");
}
}
public void readSocket()
{
try
{
BufferedReader serverBuffer = new BufferedReader(
new InputStreamReader(skt.getInputStream()));
//PrintStream ps = new PrintStream(file)

String buff = serverBuffer.readLine();
System.out.println("Printing data for Client : " + buff);
if (buff != null)
{
System.out.println("Received from Client : " + buff);
}
else
{
System.out.println("Server Buffer is empty");
}

} catch (Exception e)
{
System.out.println("Error while performing read socket for server");
}

}
public void closeServer()
{
try
{
System.out.println("Closing server socket");
socketHandler.close();
} catch (Exception e)
{
System.out.println("Error while Closing server socket");
}
}
}

-----------------------------------------------X-----------------------------------------------------------------X--------------
//Main Class for Client
package com.crude.transport;

public class MainClass
{
public static void main(String[] args)
{
//ServerSocketCommunication sc = new ServerSocketCommunication();
ClientSocketCommunication cc = new ClientSocketCommunication();

/*sc.prepareServer();
sc.readSocket();
*/
cc.prepareSocket();
cc.sendData();

cc.closeSocket();

}

}

-----------------------------------------------X-----------------------------------------------------------------X--------------
//Main Class for Server

package com.crude.transport;

public class MainForServer
{

/**
* @param args
*/
public static void main(String[] args)
{
ServerSocketCommunication sc = new ServerSocketCommunication();
sc.prepareServer();
sc.readSocket();
//sc.closeServer();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 16 2012
Added on Jan 19 2012
5 comments
242 views