Sending Datagram Packets in Java - java

I am writing a program to send an integer (variable called intToSend) over a network using UDP. I run the program on two machines on the same network, one after the other. I thought that after running them both, the first one to be run would open a message box with the sent integer, but this doesn't happen. Both programs wait for a packet to be received as shown by "Waiting..." being printed to the console. I have the program ask for the destination ip to be input to the console. Then after that, the createSocket method is called, followed by sendData and then receiveData.
Here is the code:
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
Scanner s = new Scanner(System.in);
PrintStream o = System.out, e = System.err;
InetAddress thisAddr, destAddr;
DatagramSocket socket;
int port = 1729, intToSend = 8;
boolean running = true;
public static void main(String[] args) {
new Main();
}
private Main() {
try {
thisAddr = InetAddress.getLocalHost();
System.out.println("Internal IP: " + thisAddr.getHostAddress().toString());
System.out.println("External IP: " + getIp());
} catch (Exception e) {
e.printStackTrace();
}
try {
destAddr = InetAddress.getByName(getDestIp());
} catch (UnknownHostException e) {
e.printStackTrace();
}
createSocket();
sendData();
receiveData();
}
private void receiveData(){
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
while(true){
System.out.println("Waiting...");
try {
socket.receive(receivePacket);
} catch (IOException e) {
e.printStackTrace();
}
String receivedText = new String(receivePacket.getData());
JOptionPane.showMessageDialog(null, receivedText);
}
}
private void sendData(){
byte[] dataToSend = String.valueOf(intToSend).getBytes();
DatagramPacket packet = new DatagramPacket(dataToSend, dataToSend.length, destAddr, port);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
private void createSocket(){
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
public static String getIp() throws IOException{
URL whatismyip = new URL("http://icanhazip.com");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
return in.readLine();
}
private String getDestIp() {
String temp;
o.println("What is the other user's ip?");
temp = s.nextLine();
return temp;
}
}

This code works for me. If I input the target IP as the local machine's IP then I get the popup. If I input another machine on the network I also get the popup. My guess would be that either one of your machines has a firewall running that is blocking the incoming UDP packet, or your machines have multiple network interfaces and the IP you detect is not the one that is on the same network as the other machine.
In the former case you can disable the firewall (not a good idea if your machines are not behind a router with a firewall or are on a network which you don't have full control over) or open the specific port for incoming and outgoing UDP on both machines.
In the latter case you want to look out for the IPs presented on both machines being on the same subnet (the first three numbers being the same in the case if IPv4) e.g. both starting with 192.168.1. or similar.
When you do get your packet through you will probably get a very long popup window because you allocate a 1024 byte array and put the string at the start of that array but then convert the entire 1024 byte array into a string which may include various stuff off the end of the first N bytes you wrote the int into.
There are various ways to resolve that but this but a simple way to be able to pack a bunch of data into a packet and then read it back reliably is to use ByteArrayInput/OutputStreams and DataInput/OutputStreams:
//Sending side
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeInt(N); //this will write 4 bytes
byte[] packetData = bout.toByteArray();
//Receiving side
byte[] packetBuffer = ...;
ByteArrayInputStream bin = new ByteArrayInputStream(packetBuffer);
DataInputStream din = new DataInputStream(bin);
int N = din.readInt(); //This will read only the first 4 bytes, and use the same marshalling as DataOutputStream to produce a consistent value, even if the integer value is something exceptional like infinity or NaN.

Related

Run running TCP and UDP server at the same time

I have a network programming topic that requires file transfer using TCP and UDP. If TCP send fails then UDP will be executed. I have built each part but I am not sure how can I run the server of TCP and UDP at the same time to be able to receive data of both protocols (my problem is starting 2 server in the master server because I have as the interface). Hope everybody help please .
In one case you need to open a ServerSocket, in the other case a DatagramSocket. It should be possible to open them in parallel, which means you can run both your implementations in parallel running on different threads.
If you are suppose to run the TCP and the UDP Server on the same machine, you will need to use different ports. Then you can start up two different thread, one for each server:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
public class TcpUdpServer {
private final static int UDP_PORT = 8100;
private final static int TCP_PORT = 8200;
public static void main(String[] args) {
new Thread(() -> executeTcpServer()).start();
new Thread(() -> executeUdpServer()).start();
}
public static void executeTcpServer() {
try (ServerSocket serverSocket = new ServerSocket(TCP_PORT)) {
while (true) {
System.out.println("waiting for TCP connection...");
// Blocks until a connection is made
final Socket socket = serverSocket.accept();
final InputStream inputStream = socket.getInputStream();
String text = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
System.out.println(text);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void executeUdpServer() {
try (DatagramSocket socket = new DatagramSocket(UDP_PORT)) {
while (true) {
byte[] packetBuffer = new byte[2024];
final DatagramPacket packet = new DatagramPacket(packetBuffer, packetBuffer.length);
System.out.println("waiting for UDP packet...");
// Blocks until a packet is received
socket.receive(packet);
final String receivedPacket = new String(packet.getData()).trim();
System.out.println(receivedPacket);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}

Converting java.net.Socket.getInputStream() to byte[] showing large delay

I have designed a Java Client class that is required to send a byte[] array to a Java Server class via a socket. Here is my code:
ByteArrayClient.java
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ByteArrayClient {
public static void main(String[] args) {
//make POJO__________________________
ByteEncodeSubscriptionReq sr1=ByteEncodeSubscriptionReq.makeRequest(103, "Str1", "Str2");
//Connection details____________________
String serverName = "localhost";
int port = 6060;
try {
//Establish Connection with server_______________________________
System.out.println("ByteArrayClient: Connecting to " + serverName +":" + port+"...");
Socket client = new Socket(serverName, port);//make new socket
System.out.println("ByteArrayClient: connected to " + client.getRemoteSocketAddress());
//Encode POJO to ByteArray________________________________
byte[] SubscripReqByteArray=ByteEncodeSubscriptionReq.encode(sr1);
//encoded correctly to a 44 bit byte array
System.out.println("ByteArrayClient: SubscripTionRequest successfully encoded");
//Send POJO ByteArray to server__________________________
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.write(SubscripReqByteArray);;
System.out.println("ByteArrayClient: POJO sent to server");
//Receive Server response_________________________________
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("ByteArrayClient received: " + in.readUTF());
//close socket____________________________________
client.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("PojoClient: Connection Failed");
}
}
}
...and ByteArrayServer.java
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class ByteArrayServer extends Thread{
private ServerSocket serverSocket;
public ByteArrayServer(int port) throws IOException {
serverSocket = new ServerSocket(port);//create server socket
serverSocket.setSoTimeout(15000);//socket closes after 15 seconds
this.start();
}
public void run() {
while (true) {//server runs infinitely______________
try {
System.out.println("ByteArrayServer: Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket servedClient = serverSocket.accept();//client socket
System.out.println("ByteArrayServer: connected to " + servedClient.getRemoteSocketAddress());
//Receive Client ByteArray___________________________________________
ByteEncodeSubscriptionReq receivedReq=new ByteEncodeSubscriptionReq();//server side POJO
System.out.println("ByteArrayServer: created SubscriptionReq Object");
InputStream PojoStreamHolder = servedClient.getInputStream();
System.out.println("ByteArrayServer: client InputStream received");
byte[] clientByteStream=new byte[44];//same size as Pojo byte requirement
_____/*MY CODE IS STUCK SOMEWHERE HERE*/__________
servedClient.getInputStream().read(clientByteStream);
System.out.println("ByteArrayServer: clientByteStream received: "+clientByteStream[0]+" "+clientByteStream[1]);
receivedReq=ByteEncodeSubscriptionReq.decode(clientByteStream);
//Send confirmation to Client__________________________________________________
DataOutputStream out = new DataOutputStream(servedClient.getOutputStream());
if(receivedReq.getRequestSymbol().trim().length()!=0){
out.writeUTF("ByteArrayServer received Subscription ID="+receivedReq.getSubscriptionID());
System.out.println("ByteArrayServer: new SubscriptionRequest ID="+receivedReq.getSubscriptionID()+" Subscriber_Name="+receivedReq.getSubscriberName());
}else{
out.writeUTF("ByteArrayServer: did not receive Subscription ID");
}
//Close Client socket_________________________________________________________
//server.close();
//serverSocket.close();
} catch (SocketTimeoutException s) {
System.out.println("PojoServer: Socket timed out after " + getTimeElapsedInSeconds(startTime) + " seconds from start");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
// int port = Integer.parseInt(args[0]);//to get port as an Argument
int port = 6060;
try {
Thread t = new ByteArrayServer(port);
startTime = System.currentTimeMillis();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the Server console output:
ByteArrayServer: Waiting for client on port 6060...
ByteArrayServer: connected to /127.0.0.1:64233
ByteArrayServer: created SubscriptionReq Object
ByteArrayServer: client InputStream received
The issue is that while the Stream is received by the server without errors, it gets stuck near servedClient.getInputStream().read(clientByteStream); method and does not proceed further.
I've also tried
int count=servedClient.getInputStream().read(clientByteStream);
and
DataInputStream in = new DataInputStream(servedClient.getInputStream());
long bStr=in.readLong();
and
ObjectInputStream PojoObjHolder = new ObjectInputStream(PojoStreamHolder);
byte[] clientByteStream2 = (byte[])PojoObjHolder.readObject();
..but they show the same problem as well.
How should I pass the Byte Array between the two classes without extra imports?
The problem was in my ByteArrayClient Class. I had to link the OutputStream with the client socket, rather than creating a new instance of it. So I replaced:
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.write(SubscripReqByteArray);;
with
OutputStream os = client.getOutputStream(out);
os.write(SubscripReqByteArray);;
Thank you for the hint Ekant
DataInputStream.readFully(byte[] b) will finish only when in inputstream bytes till b.length available. So for sure you need to debug if you have all the bytes or not.And the solution is to make those byte available so that the function will finish.
Same for DataInputStream.read(byte[] b) The method is blocked until input data is available.
Please make sure by debugging your app that inputstream have 44 bytes.
Try below to count available bytes and you can read those easily.
// count the available bytes form the input stream
int count = is.available();
// create buffer
byte[] bs = new byte[count];
// read data into buffer
dis.read(bs);

Java multicast client server

I try to create a program which is based on client/server communication.
In this program I have several clients and several servers.
At the beginning of the program some clients go up, and wait for new connections from different servers.
When the first server goes up, I want him to know who is the clients that are available in the network, and then it begins to communicate with all these clients in tcp protocol (ServerSocket).
However, if a new clients goes up, this server should know about it.
The same happens when new servers go up.
My first think, is to use multicast commnication. The client will send every 5 seconds a broadcast to the networks and the servers will listen to these messages.
My broblem is how to combine these two methods (multicast and tcp) in order to built this program.
Should I use two differnet ports one for the multicast, and one for the tcp protocol?
Is it possible to allow the server\client to listen for different communication (multicast or tcp) with treads?
I just need a litle push, and some basic code in order to begin this...
EDIT: OK, I succeeded to built a multicast comunication.
The agent goes up, and send every 5 seconds its name.
the server goes up, and receives the agent's broadcast.
If a new agent goes up, the server know about it.
The code look like this:
Agent code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
public class Agent {
private static int portMulticasting = 1004;
private DatagramSocket socket;
private boolean broadcast = true;
private String group = "230.0.0.1"; //group address
private int delay = 5000;
public Agent(){
try{
socket = new DatagramSocket();
System.out.println("agent ready");
}
catch (SocketException e){
e.printStackTrace();
System.exit(1);
}
}
public void start(String agentName){
DatagramPacket packet;
try{
InetAddress address = InetAddress.getByName(group);
while (broadcast){
byte[] buf = new byte[256];
buf = agentName.getBytes();
packet = new DatagramPacket(buf,buf.length,address,portMulticasting);
socket.send(packet);
try{
Thread.sleep(delay);
} catch (InterruptedException e){
System.exit(0);
}
}
socket.close();
} catch (IOException e){
e.printStackTrace();
}
}
public static void main (String[] args) throws IOException {
System.out.println("Enter name of the new agent: ");
Scanner sc = new Scanner(System.in);
String agentName = sc.nextLine();
Agent agent = new Agent();
agent.start(agentName);
}
}
Server Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
String groupIP = "230.0.0.1";
int portMulticasting = 1004;
if (args.length > 0)
groupIP = args[0];
try{
//get a multicast socket and join group
MulticastSocket socket = new MulticastSocket(portMulticasting);
InetAddress group = InetAddress.getByName(groupIP);
socket.joinGroup(group);
//get packet
DatagramPacket packet;
while (true){
byte[] buf = new byte[256];
packet = new DatagramPacket(buf,buf.length);
socket.receive(packet);
buf = packet.getData();
int len = packet.getLength();
String received = (new String(buf)).substring(0,len);
try{
System.out.println("Agent name: " + received);
} catch (NumberFormatException e){
System.out.println("cannot interpret number");
}
}
socket.leaveGroup(group);
socket.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
Now, I want that the agents continue to send broadcast every 5 seconds, but meanwhile the server begin to work with the agents (send messages, ask for information from the agents etc).

Java - Download a file through browser using a Socket

i was studying Java Socket and i tried to develop a Socket using port 80 to download a file from browser.
So, i run my main class (source below), it will open a Socket in any port i want to.
Then someone outside will access http://MY_IP:MY_PORT/download/FILE_NAME
I got this all working, however the filesize on client side is 0 bytes (for small files), and slightly lower size for bigger archives (original 600mb, download 540mb+-)
I really checked my code a lot of times, i couldn't find any error, i also changed from java libs to Apache-commons thinking it would help, but no success.
so maybe i think i got something wrong on Response headers.
Can you guys help me please?
Thanks in advance.
Class HTTPDownload:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class HTTPDownloader {
Socket incoming = null;
ServerSocket server = null;
public HTTPDownloader(){
int port = 11000;
try{
server = new ServerSocket(port);
System.out.println("Creating SocketServer on Port " + port);
}catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Preparing to accept connections...");
while(true){
try{
incoming = server.accept();
System.out.println("connection!");
HTTPDownloaderThread thread1 = new HTTPDownloaderThread(incoming);
thread1.start();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException{
new HTTPDownloader();
}
}
Class HTTPDownloadThread:
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Paths;
class HTTPDownloaderThread extends Thread {
private static final int BUFFER_SIZE = 4096;
private Socket socket;
private byte[] buf = new byte[BUFFER_SIZE];
private OutputStream out;
private InputStream is;
HTTPDownloaderThread(final Socket socket){
this.socket = socket;
}
public void run(){
int numberRead = 0;
try{
out = socket.getOutputStream();
is = socket.getInputStream();
numberRead = is.read(buf, 0, BUFFER_SIZE);
System.out.println("read " + numberRead);
if(numberRead<0)
return;
byte[] readBuf = new byte[numberRead];
System.arraycopy(buf, 0, readBuf, 0, numberRead);
String header = new String(readBuf);
System.out.println(header);
String fileName = header.split("\r\n")[0].split(" ")[1].substring(1);
System.out.println(socket.getInetAddress().getHostAddress()+" asked for file: "+fileName);
File f = new File("C:\\TestFolder\\"+fileName);
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Accept-Ranges: bytes\r\n".getBytes());
out.write(("Content-Length: "+f.length()+"\r\n").getBytes());
out.write("Content-Type: application/octet-stream\r\n".getBytes());
out.write(("Content-Disposition: attachment; filename=\""+fileName+"\"\r\n").getBytes());
out.write("\r\n".getBytes()); // Added as Joy RĂȘ proposed, make it work!
Files.copy(Paths.get("C:\\TestFolder\\"+fileName) , out);
System.out.println("File upload completed!");
// out.flush();
out.close();
socket.close();
}catch(SocketException e) {
System.out.println(e.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
}
For one thing, add another "\r\n" between headers and data. Check your HTTP Response; does the Content-Length header report the correct file size for the downloaded file? Do the files show up usable on the client in the same way they were on the server?
Web proxies always helpful in debugging HTTP (or other client-server) applications :)
Also, I'm assuming you are specifying port 11000 on the browser, as that's what you're listening on on the server
The website does not let me to comment but i thought that I should tell about my findings.. By using
Files.copy("path",outStreamObj);
outStreamObj.close();
socketObj.close();
Will cause incomplete or corrupt downloads but if still want to use then outStreamObj and socketObj must not be closed the files transfer is fast with the above code (atleast from my observation). If you try to close it will report Broken Pipe or Connection reset or will not complete the download(freeze it).
Instead using the following code will let you close the outStreamObj as socketObj but file download is slow from the socket probably because of while loop.
Socket socket=serverSocket.accept();
FileInputStream fs=new FileInputStream(path);
OutputStream out = socket.getOutputStream();
//This is the change from the Files.copy()
int reads=0;
while((reads=fs.read())!=-1)
{
out.write(reads);
}
out.close();
socket.close();

Java Sockets: My server input stream will not read from the client output stream?

EDIT: Ik it is long but does anyone know how to program sockets??
My problem is confusing me a bit. I have a server running on one computer and on another, I have a client connected to it. When I type a message from the client into the console and send it, the server does not seem to receive it. Anybody know why because I have been testing with printing to the console for the last 3 hours and cannot figure this out. I am relatively new to sockets so don't be too harsh if I am just being an idiot.
Heres my code for the client side:
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class SocketClient {
public static void main(String [] args) {
String host = "************";
int port = 25565;
StringBuffer instr = new StringBuffer();
String TimeStamp;
System.out.println("SocketClient initialized");
try {
InetAddress address = InetAddress.getByName(host);
Socket connection = new Socket(address, port);
BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
Scanner scan = new Scanner(System.in);
String message = scan.nextLine();
TimeStamp = new java.util.Date().toString();
String process = "Server called on " + host + ":" + port + " at " + TimeStamp + ": " + message + (char) 13;
osw.write(process);
osw.flush();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
int c;
while ( (c = isr.read()) != 13)
instr.append( (char) c);
connection.close();
System.out.println(instr);
} catch (UnknownHostException e) {
System.err.println("UnknownHostException: " + e);
} catch (IOException e) {
System.err.println("IOExcepion: " + e);
}
}
}
Here is the code to connect the client to the server:
import java.io.IOException;
import java.net.*;
public class MultipleSocketServer {
public static Socket connection;
public static String name = "Tyler's Server";
public static int limit = 2;
public static Thread[] clients = new Thread[limit];
public static int current = 0;
public static int port = 25565;
public static String[] connected = {"", ""};
public static ServerSocket socket;
public static void main(String[] args) {
System.out.println("Server starting...");
try {
ServerSocket socket = new ServerSocket(port);
while(true) {
Socket connection = socket.accept();
String ip = connection.getRemoteSocketAddress().toString().substring(1, 13);
loop:
for(int i = 0; i < connected.length; i++) {
if(connected[0].equals(ip) || connected[1].equals(ip)) {
break loop;
}else if(!connected[i].equals(ip)) {
connected[i] = ip;
System.out.println(ip);
MultiServer_Client client = new MultiServer_Client(connection, i);
Thread run = new Thread(client);
run.start();
break loop;
}
}
}
} catch (IOException e1) {
System.out.println("Could not bind server on: " + port);
System.exit(-1);
}
}
}
And here is my code to handle each client as connected:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class MultiServer_Client implements Runnable {
public String time;
public Socket client;
public StringBuffer process = new StringBuffer();
public BufferedInputStream inputStream;
public InputStreamReader reader;
public BufferedOutputStream outputStream;
public OutputStreamWriter writer;
public boolean connected = true;
public int ID;
public MultiServer_Client(Socket connection, int i) {
client = connection;
ID = i;
try {
inputStream = new BufferedInputStream(client.getInputStream());
reader = new InputStreamReader(inputStream);
outputStream = new BufferedOutputStream(client.getOutputStream());
writer = new OutputStreamWriter(outputStream, "US-ASCII");
} catch (IOException e) {
System.out.println("IOException: " + e);
}
System.out.println("Client connected...");
write("Connected to " + MultipleSocketServer.name);
}
public void run() {
while(connected) {
write("hi");
}
System.out.println("Disconnecting client...");
}
public void write(String authen) {
try {
time = new java.util.Date().toString();
String message = time + ": " + authen + (char) 13;
writer.write(message);
writer.flush();
} catch (IOException e) {
connected = false;
MultipleSocketServer.connected[ID] = "";
}
}
public void read() {
//read from client
int character;
process = new StringBuffer();
try {
while ((character = reader.read()) != 13) {
process.append((char) character);
}
System.out.println(process);
process.delete(0, process.length());
} catch (IOException e) {
connected = false;
MultipleSocketServer.connected[ID] = "";
}
}
}
Srry if I cannot help very much. As I said, I am new to sockets and no one else seems to have any problems with this... Thanks :)
The problem with your code is not the "sockets" its your communication protocol. You are effectively closing the socket before the server has a chance to write out "hi".
To debug this, you want to reduce the complexity of your program. There are a number of things that don't make any sense or matter in your program.
So, a little background on Sockets. There are two types of sockets. A "ServerSocket" and a "Socket" The ServerSocket is sort of like a secretary. Its only job is to listen for calls and then pass them on. This is what the "accept" does. Before any client connects, the accept() will block until it receives a connection. Once the client connects, the accept returns a Socket representing the connection.
The regular Socket is where all the work occurs. You can think of it as a telephone connection. You can talk to someone remotely with the OutputStream, and listen using the InputStream. The challenge is that you need to create some sort of communication (called a protocol) for your two sockets to communicate.
You need to figure out how you want to delimit your commands. You could pass a fixed length number and then the data if you want a "length" delimited protocol or you could use a special character for the end of the message (what you currently have). For the quick and dirty, I often use the latter with a newline character. The easiest is to just use a PrintWriter for writing and a Scanner for reading.
The next step is to figure out the communication pattern for the client and the server. Think if it as passing a ball back and forth. If the client says something, the other side should be listening (and vice versa).
Once the protocol and logic is figured out, you can then move the logic for "handling" the server side into separate threads (called a worker pattern) so that the server can handle more than one client at a time. If you want to go even further, you can implement a reactor with a thread pool so that the server doesn't run out of threads, but that is probably for another day/question.
I would recommend following the Java tutorial on Sockets: http://docs.oracle.com/javase/tutorial/networking/sockets/index.html

Categories