Vous êtes sur la page 1sur 5

G. H.

Patel College of Engineering and Technology


Subject : - Advance JAVA(2160707)
Lab 1
Aim: 1. Implement TCP Server for transferring files using Socket and ServerSocket.
Program Code:
Client:
import java.net.*;
import java.io.*;
import java.lang.*;

class client{
public static void main(String a[]) throws IOException{
try{
Socket s=new Socket("localhost",1234);
FileOutputStream fos=new FileOutputStream
("filetransfer.txt");
InputStream is=s.getInputStream();
byte b[]=new byte[5000];
int i=is.read(b,0,b.length);
DataOutputStream dos=new DataOutputStream(fos);
dos.write(b,0,i);
DataInputStream dis=new DataInputStream
(s.getInputStream());
String or=dis.readUTF();
System.out.println(or);
s.close();
}
catch(Exception e)
System.out.println(e);
}
}

160110107015 |
G. H. Patel College of Engineering and Technology
Subject : - Advance JAVA(2160707)
Lab 1
Server:

import java.net.*;
import java.io.*;
import java.lang.*;

class server{
public static void main(String a[]) throws IOException{
try{
ServerSocket ss=new ServerSocket(1234);
Socket s=ss.accept();
File f=new File("file1.txt");
FileInputStream fis=new FileInputStream(f);
DataInputStream dis=new DataInputStream(fis);
int f1=(int)f.length();
byte b[]=new byte[f1];
int i=dis.read(b,0,b.length);
OutputStream os=s.getOutputStream();
os.write(b,0,i);
DataOutputStream dos=new DataOutputStream
(s.getOutputStream());
String str="File Created Sucessfully";
dos.writeUTF(str);
s.close();
}
catch(Exception e)
System.out.println(e);
}
}

160110107015 |
G. H. Patel College of Engineering and Technology
Subject : - Advance JAVA(2160707)
Lab 1
Output:

Aim: 2. Write program in which server listen on port for incoming


connection from client program. When server connects to a client it reads
one line of text from client, reverse chars in a line and sends them back to
the client.

Program Code:

Client:
import java.net.*;
import java.io.*;
import java.lang.*;

160110107015 |
G. H. Patel College of Engineering and Technology
Subject : - Advance JAVA(2160707)
Lab 1

class client{
public static void main(String a[]) throws IOException{
try{
Socket s=new Socket("localhost",1234);
DataOutputStream dos=new DataOutputStream
(s.getOutputStream());
String str="hello i am pratik";
dos.writeUTF(str);
DataInputStream dis=new DataInputStream
(s.getInputStream());
System.out.println(dis.readUTF());
s.close();
}
catch(Exception e)
System.out.println(e);
}
}

Server:

import java.net.*;
import java.io.*;
import java.lang.*;

class server{
public static void main(String a[]) throws IOException{
try{
ServerSocket ss=new ServerSocket(1234);
Socket s=ss.accept();

160110107015 |
G. H. Patel College of Engineering and Technology
Subject : - Advance JAVA(2160707)
Lab 1
DataInputStream dis=new DataInputStream
(s.getInputStream());
String or=dis.readUTF();
System.out.println(or);
StringBuilder sb=new StringBuilder(or);
String re=sb.reverse().toString();
DataOutputStream dos=new DataOutputStream
(s.getOutputStream());
dos.writeUTF(re);
s.close();
}
catch(Exception e)
System.out.println(e);
}

Output:

160110107015 |

Vous aimerez peut-être aussi