For client:
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String[] args) throws IOException {
//Open your connection to a server, at port 1234
Socket s1 = new Socket("", 1234);
//Get an input file handle from the socket and read the input
InputStream s1in = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1in);
String st = new String(dis.readUTF());
System.out.println(st);
//When done just close the connection and exit
dis.close();
s1in.close();
s1.close();
}
}
For Server:
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String[] args) throws IOException{
//Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1 = s.accept();//wait and accept a connection
//Get a connection stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
//Send a string
dos.writeUTF("Hi there");
//close the connection but not the server socket
dos.close();
s1out.close();
s1.close();
}
}
import java.io.*;
public class SimpleClient {
public static void main(String[] args) throws IOException {
//Open your connection to a server, at port 1234
Socket s1 = new Socket("", 1234);
//Get an input file handle from the socket and read the input
InputStream s1in = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1in);
String st = new String(dis.readUTF());
System.out.println(st);
//When done just close the connection and exit
dis.close();
s1in.close();
s1.close();
}
}
For Server:
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String[] args) throws IOException{
//Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1 = s.accept();//wait and accept a connection
//Get a connection stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
//Send a string
dos.writeUTF("Hi there");
//close the connection but not the server socket
dos.close();
s1out.close();
s1.close();
}
}
No comments:
Post a Comment