Implementing a java program for creating a socket for HTTP for Webpage upload and Download. I wrote a server side program ans client program. when i run the program it says Native port problem.
Server.Java
package javaapplication1;
import java.io.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
public class Server {
public static void main(String[] args)throws Exception {
ServerSocket server= null;
Socket socket;
server=new ServerSocket(4000);
System.out.println("Server waiting for image");
socket=server.accept();
System.out.println("Client Connected");
InputStream in=socket.getInputStream();
DataInputStream dis=new DataInputStream(in);
int len=dis.readInt();
System.out.println("Image size:"+len/1024+"kb");
byte[] data=new byte[len];
dis.readFully(data);
dis.close();
in.close();
InputStream ian=new ByteArrayInputStream(data);
BufferedImage bImage=ImageIO.read(ian);
JFrame f=new JFrame("server");
ImageIcon icon=new ImageIcon(bImage);
JLabel l=new JLabel();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
}
}
Client.Java
package javaapplication1;
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author Mohamed
*/
public class Client {
public static void main(String[] args)throws Exception {
Socket soc;
BufferedImage img= null;
soc=new Socket("localhost",4000);
System.out.println("Client running");
try
{
System.out.println("Reading image from disk");
img=ImageIO.read(new File("blue.jpg"));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(img,"jpg",baos);
baos.flush();
byte[] bytes=baos.toByteArray();
baos.close();
System.out.println("Sending image to server");
OutputStream out=soc.getOutputStream();
DataOutputStream dos=new DataOutputStream(out);
dos.writeInt(bytes.length);
dos.write(bytes,0,bytes.length);
System.out.println("Image sent to server");
dos.close();
out.close();
}
catch(Exception e)
{
System.out.println("Exception:"+e.getMessage());
soc.close();
}
soc.close();
}
}
And the error for the Client.Java is in the Image below Error message for the code Client.Java
What the actual out should be when the code is excuted in Command Prompt is as follows
C:\Program Files\Java\jdk.1.6.0_21\bin>javac Server.java
C:\Program Files\Java\jdk.1.6.0_21\bin>java Server
Server waiting for image
Client connected
Image size:27kb
C:\Program Files\Java\jdk.1.6.0_21\bin>javac Client.java
C:\Program Files\Java\jdk.1.6.0_21\bin>java Client
Client is running
Reading image from disk
Sending Image to server
Image sent to the server
<And the image is displayed>
Please tell me what is the error or leave a comment with correct code. Thank you!