The Artima Developer Community
Sponsored Link

Java Answers Forum
help me ..

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
tareliang

Posts: 1
Nickname: tareliang
Registered: Oct, 2002

help me .. Posted: Oct 29, 2002 4:03 AM
Reply to this message Reply
Advertisement
it is my first client-server program..but i don`t
know how to use two ( or more ) client to connect
server..( only one client can connect ) hope everyone
teach me , i will feel very very thankful..

***below is client code ***

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

public class client {
public static void main (String args[]) {
try {
if (args.length != 1) {
System.out.println("WARING !! No IP address exist!");
return; }
String strargs = args[0];
Socket socket;
if (strargs.equals("localhost"))
socket = new Socket(InetAddress.getLocalHost(),6000);
else
socket = new Socket(InetAddress.getByName(strargs),6000);

InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
OutputStream out = socket.getOutputStream();
PrintWriter pw = new PrintWriter(out,true);
String info;
info = br.readLine();
System.out.println(info);
boolean done = false;
BufferedReader strInput = new BufferedReader(new InputStreamReader(System.in));
String strOutput;
while(!done) {
strOutput = strInput.readLine();
pw.println(strOutput);
if (strOutput.equalsIgnoreCase("bye")) done = true;
info = br.readLine();
System.out.println(info); }
socket.close(); }
catch(SecurityException e) {}
catch(IOException e) {} } }


***below is server code ***

import java.io.*;
import java.net.*;
import java.util.Hashtable;

class GCD_Thread extends Thread {
int intGCD,intGCD1,intGCD2,intGCD3,intGCD4;
public int getGCD() {
return intGCD; }
GCD_Thread(int tmpi,int tmpj) {
intGCD1=tmpi;
intGCD2=tmpj; }
public void run() {
try {
Thread GCD_Thread=Thread.currentThread();
GCD_Thread.setName("GCD");
System.out.println((Thread.currentThread()).getName()+
" thread execute... ");
intGCD3=intGCD1%intGCD2;
intGCD4=intGCD1*intGCD2;
while (intGCD3!=0) {
intGCD1=intGCD2;
intGCD2=intGCD3;
intGCD3=intGCD1%intGCD2; }
intGCD=intGCD2;
System.out.println("Calculation GCD is "+intGCD);
Thread.sleep(500); }
catch (InterruptedException e) {}
System.out.println((Thread.currentThread()).getName()+" thread ending."); }}

class LCM_Thread extends Thread {
int intLCM,intLCM1,intLCM2,intLCM3,intLCM4;
public int getLCM() {
return intLCM; }
LCM_Thread(int tmpi,int tmpj) {
intLCM1=tmpi;
intLCM2=tmpj; }
public void run() {
try {
Thread LCM_Thread=Thread.currentThread();
LCM_Thread.setName("LCM");
System.out.println((Thread.currentThread()).getName()+" thread execute... ");
intLCM3=intLCM1%intLCM2;
intLCM4=intLCM1*intLCM2;
while (intLCM3!=0) {
intLCM1=intLCM2;
intLCM2=intLCM3;
intLCM3=intLCM1%intLCM2; }
intLCM=intLCM4/intLCM2;
System.out.println("Calculation LCM is "+intLCM);
Thread.sleep(500); }
catch (InterruptedException e) {}
System.out.println((Thread.currentThread()).getName()+" thread ending."); } }

public class server {
public static void main (String args[]) {
try {
ServerSocket ss = new ServerSocket(6000);
Socket socket = ss.accept();
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
OutputStream out = socket.getOutputStream();
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("Connecting SERVER.. press two int values to calculation GCD & LCM");
boolean done = false;
while (!done) {
String line = br.readLine();
if (line == null) done = true;
if (line.trim().equals("bye")) done = true;
else {
Thread thread=Thread.currentThread();
thread.setName("Main");
System.out.println((Thread.currentThread()).getName()+" thread execute... ");
int inti=line.length(),intj=line.indexOf(",");
int GCD,LCM;
String str1=line.substring(0,intj++);
String str2=line.substring(intj,inti);
int inta=(Integer.valueOf(str1)).intValue();
int intb=(Integer.valueOf(str2)).intValue();
GCD_Thread GCD_Thread=new GCD_Thread(inta,intb);
new Thread(GCD_Thread).start();
LCM_Thread LCM_Thread=new LCM_Thread(inta,intb);
new Thread(LCM_Thread).start();
Thread.sleep(2000);
GCD=GCD_Thread.getGCD();
LCM=LCM_Thread.getLCM();
System.out.println("In Main thread get GCD value is "+GCD);
System.out.println("In Main thread get LCM value is "+LCM);
String strGCD = String.valueOf(GCD);
String strLCM = String.valueOf(LCM);
String strResulr=strGCD+" "+strLCM;
pw.println("The GCD & LCM is .."+strResulr);
Thread.sleep(1000);
System.out.println((Thread.currentThread()).getName()+" thread Ending. "); } }
ss.close(); }
catch (Exception e) {} } }

Topic: using C routine in Java Previous Topic   Next Topic Topic: String reversal

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use