|
||
|
|||
|
i hv done a program on a server client program on my own machine...i m posting the code..what i want to do is what i type in client site will be seen on the server site too..pls help me...
Client... Java Code
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) throws Exception {
Socket cs = new Socket("localhost",6666);
int flag=1;
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Client:");
ObjectOutputStream oos=new ObjectOutputStream(cs.getOutputStream());
while(flag==1)
{
//String str = dis.readLine();
//System.out.println(str);
oos.writeObject(dis.readLine());
if((str=="bye")||(str=="BYE"))
flag=0;
}
oos.close();
dis.close();
cs.close();
}
}
Server... Java Code
import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(6666);
Socket ss1=ss.accept();
boolean flag=true;
DataInputStream dis=new DataInputStream(ss1.getInputStream());
System.out.println("connection established......");
System.out.println("server:");
ObjectOutputStream ps=new ObjectOutputStream(ss1.getOutputStream());
while(flag==true){
String str=dis.readLine();
System.out.println(str);
if((str=="bye")||(str=="BYE"))
flag=false;
}
System.out.println("connection closed....");
ps.close();
ss1.close();
ss.close();
}
}
Last edited by Freaky Chris; 16-12-2009 at 09:51 AM. |
|
|||
|
I think the easiest way to get data from point A to point B trough socket is using DataInputStream and DataOutputStream
DataInputStream (Java 2 Platform SE v1.4.2) DataOutputStream (Java 2 Platform SE v1.4.2) Last edited by Koâk; 16-12-2009 at 06:05 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Exceptions with my simple Client and server programs | Dko | Exceptions | 0 | 13-12-2009 02:09 AM |
| Simple server-client trouble | DC200 | Java Networking | 3 | 12-11-2009 12:16 PM |
| how compose in the top of the same machine since is a client is also serve | NARs | Java Applets | 2 | 23-10-2009 07:34 AM |
| Download a file to client from server | mvittalreddy | Java Networking | 4 | 05-10-2009 09:23 AM |
| instead of printing in the client, report get printed in the server | jmvenkat | Java Theory & Questions | 0 | 19-09-2009 06:30 AM |