SIMPLE CLIENT AND
SERVER PROGRAMMING USING SOCKETS IN JAVA
Server socket:
import
java.net.ServerSocket;
import
java.net.Socket;
ServerSocket ss=new ServerSocket(6655);
jLabel1.setText("SERVER
STARTED....");
Socket s=ss.accept(); //WAIT TILL CONNECTION RECEIVED FROM CLIENT..
jLabel1.setText("SERVER CONNTECTED WITH CLIENT....");
DataInputStream dis=Input();
DataOutputStream dout=output();
String type=dis.readUTF();
System.out.Println(type);
public DataInputStream Input() throws IOException
{
DataInputStream dis=new DataInputStream(s.getInputStream());
return dis;
}
public DataOutputStream output() throws IOException
{
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
return dout;
}
CLIENT SOCKET:-
Streams.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Streams
{
//Socket s;
public
DataInputStream Input(Socket s) throws IOException
{
DataInputStream dis=new DataInputStream(s.getInputStream());
return dis;
}
public
DataOutputStream output(Socket s) throws IOException
{
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
return dout;
}
}
USING STREAMS IN
NATIVE CODE:
NativecodecLIENT.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
String host=”localhost”;
Socket s =new
Socket(host,6655);//ESTABLISH CONNECTION TO SERVER WITH IP 127.0.0.0.1
DataInputStream dis=new Streams().Input(s);
DataOutputStream dout=new Streams().output(s);
dout.writeUTF("data
variable 1");
No comments:
Post a Comment