Professional Documents
Culture Documents
// Server
import java.io.*;
import java.net.*;
publicclass EServer
{
publicstaticvoid main(String[] args) throws IOException
{
while(true)
{
Socket Client = S.accept();
InputStream in = Client.getInputStream();
System.out.println(Dis.readUTF());
Dos.writeUTF(Str);
Client.close();
// Client
import java.io.*;
import java.net.*;
import java.util.*;
publicclass EClient
{
publicstaticvoid main(String[] args) throws IOException
{
Socket C = new Socket("localhost",3000);
BufferedReader buff = new BufferedReader(new InputStreamReader (System.in));
String Str = buff.readLine();
OUTPUT :
C:\jdk1.1.3\bin>javac EClient.java
C:\jdk1.1.3\bin>java EClient
hi, how are you Server?
Server Says :: Fine, Thankyou. Bye.
2.Write an echo program with client and concurrent server using UDP.
import java.io.*;
import java.net.*;
publicclass EServer
{
publicstaticvoid main(String[] args) throws IOException
{
while(true)
{
Socket Client = S.accept();
InputStream in = Client.getInputStream();
System.out.println(Dis.readUTF());
Dos.writeUTF(Str);
Client.close();
}
Output:
========= Server =============
C:\jdk1.1.3\bin>javac EServer.java
C:\jdk1.1.3\bin>java EServer
Client Say :: hi, how are you Server?
Fine, Thankyou. Bye.
ServerThread:
3.Write an echo program with client and concurrent server using UDP.
while true:
serverLock.acquire()
serverLock.release()
requestThread:
//handle packet
serverLock.acquire()
if numberOfRequests == MAX_REQUESTS:
numberOfRequests--
serverMonitor.pulse();
serverLock.release()
OUTPUT :
C:\jdk1.1.3\bin>javac EClient.java
C:\jdk1.1.3\bin>java EClient
hi, how are you Server?
Server Says :: Fine, Thankyou. Bye.
Output:
Waiting for clients...
Client connected
5.Write a program to retrieve date and time using TCP.
import java.net.*;
import java.io.*;
import java.util.*;
class SocketTime01{
public static void main(String[] args){
String server = "www.yourFavoriteServer.com";
int port = 13; //daytime port
try{
Socket socket = new Socket(server,port);//get socket
BufferedReader inputStream =
new BufferedReader(new
InputStreamReader(
socket.getInputStream()));
//Get an input stream from the socket
System.out.println(inputStream.readLine() +
" at " + server);//receive and display
socket.close();
}//end try
catch(UnknownHostException e){
System.out.println(e);
System.out.println(
"Connection Failure. Are you online?");
}//end catch UnknownHostException
catch(IOException e){System.out.println(e);}
}//end main
}//end class SocketTime01
Output:
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
//Server Side:
-------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
//Server Socekt Created
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
//Send Capitalized data back to client
}
}
}
Output:
Output:
Hi
}
Output:
Hi
9.Write an echo client and server program using Unix domain stream socket.
# Echo server program
import socket,os
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove("/tmp/socketname")
except OSError:
pass
s.bind("/tmp/socketname")
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
Java:
/* Client: how do you mention the /tmp/socketname? */
public static String localsendTCPBytes(String bytes) throws IOException {
String downloaded = null;
Socket socket = new Socket("localhost", 58888);
DataOutputStream upload = new DataOutputStream(socket.getOutputStream());
BufferedReader download = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
Received
downloaded
10. Write an echo client and server program using Unix domain Datagram socket.
import java.net.*;
import java.io.*;
public class InetExample {
public static void main(String [] args) {
printLocalAddress();
Reader kbd = new FileReader (FileDescriptor.in);
BufferedReader bufferedkbd = new BufferedReader (kbd);
try {
String name;
do {
System.out.print(host name or IP address: );
System.out.flush();
name = bufferedkbd.readLine();
if(name!=null)
printRemoteAddress (name);
} while (name!=null);
System.out.println(exit);
}
catch(IOException ex) {
System.out.println(Input error: );
ex.printStackTrace();
}
}
Output:
Error
class MyServer
{
ServerSocket ss;
Socket clientsocket;
BufferedReader fromclient;
InputStreamReader isr;
PrintWriter toclient;
public MyServer()
{
String str = new String("hello");
try
{
System.out.println("Got a client");
System.out.println("Client Address "+
clientsocket.getInetAddress().toString());
isr = new InputStreamReader(clientsocket.getInputStream());
fromclient = new BufferedReader(isr);
String strfile;
String stringdata;
boolean file_still_present=false;
strfile = fromclient.readLine();
System.out.println(strfile);
//toclient.println("File name received at Server is "+strfile);
if(notendcharacter!=-1)
{
file_still_present=true;
}
{
file_still_present=false;
}
}while(file_still_present);
fileinputstream.close();
System.out.println("File has been send successfully .. message print
from server");
if(str.equals("bye"))
{
break;
}
fromclient.close();
toclient.close();
clientsocket.close();
}
}
catch(Exception ex)
{
System.out.println("Error in the code : " + ex.toString());
}
}
Output:
Hello
Connection created
12. Write a client and server program to implement the remote command execution
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.session.SessionOutputReader;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
final private String username = "root";
final private String password = "pvadmin";
final private String hostname = "172.31.50.102";
private int outputPos = 0 ;
ssh = new SshClient();
try{
ssh.connect(hostname, new IgnoreHostKeyVerification());
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(username);
pwd.setPassword(password);
int result = ssh.authenticate(pwd);
f (result == 4)
{
logCat.info("Connected to host");
session = ssh.openSessionChannel();
sor = new SessionOutputReader(session);
boolean requestPseudoTerminal(String term, int cols, int rows, int width, int
height, String modes)
if (session.startShell())
{
in = (session.getInputStream());
logCat.info(in);
out = session.getOutputStream();
out.write("cd opt/3com/vcx/vcxdata/\n".getBytes());
out.write("ls\n".getBytes());
out.write("su cworks\n".getBytes()); // input command su cworks
out.write("su root\n".getBytes());
// then input command su root which will claim password
Thread.currentThread().sleep(1000*2);
//verify what we have got
String answer = null;
String aux = null;
aux = sor.getOutput(); // get the output in real time
answer = aux.substring(outputPos);
outputPos = aux.length();
logCat.info(answer);
String str[] = answer.split("\n");
String prompt = str[str.length-1]; // get the last prompt
prompt = prompt.trim();
if (prompt.contains("Password:"))
// if the last prompt is "password
{
// then we input password.
out.write("pvadmin\n".getBytes());
}
Output :
Connected host