Click here to Skip to main content
15,891,033 members
Home / Discussions / Java
   

Java

 
AnswerRe: online auction using java client server socket?? Pin
42774802-Nov-09 7:37
42774802-Nov-09 7:37 
GeneralRe: online auction using java client server socket?? Pin
golisarmi2-Nov-09 7:53
golisarmi2-Nov-09 7:53 
GeneralRe: online auction using java client server socket?? Pin
42774802-Nov-09 7:58
42774802-Nov-09 7:58 
GeneralRe: online auction using java client server socket?? Pin
golisarmi2-Nov-09 8:09
golisarmi2-Nov-09 8:09 
GeneralRe: online auction using java client server socket?? Pin
golisarmi3-Nov-09 12:27
golisarmi3-Nov-09 12:27 
GeneralRe: online auction using java client server socket?? Pin
Richard MacCutchan3-Nov-09 23:25
mveRichard MacCutchan3-Nov-09 23:25 
GeneralRe: online auction using java client server socket?? Pin
Nagy Vilmos4-Nov-09 0:24
professionalNagy Vilmos4-Nov-09 0:24 
GeneralRe: online auction using java client server socket?? Pin
golisarmi4-Nov-09 0:29
golisarmi4-Nov-09 0:29 
I myself deleted the original message by accident.

this is my server code:
mport java.net.*;
import java.io.*;
import java.util.*;

public class AuctionServer {

public AuctionServer (int port) throws IOException {

ServerSocket server = new ServerSocket (port);

while (true) {

Socket client = server.accept();
DataInputStream in = new DataInputStream(client.getInputStream());
String name = in.readUTF();
System.out.println ("New client "+name+" from " + client.getInetAddress());
AuctionHandler c = new AuctionHandler (name, client);
c.start ();
}
}

public static void main (String args[]) throws IOException {
if (args.length != 1)
throw new RuntimeException ("Syntax: java AuctionServer <port>");
new AuctionServer (Integer.parseInt (args[0]));


}
}

and client code is :

import java.net.*;
import java.io.*;
import java.awt.event.*;

public class AuctionClient {

public AuctionFrame gui;

private Socket socket;
private DataInputStream in;
private DataOutputStream out;

public AuctionClient(String name, String server, int port) {

gui = new AuctionFrame("Online Auction!!!");
gui.input.addKeyListener (new EnterListener(this,gui));
gui.addWindowListener(new ExitListener(this));


try {
socket = new Socket(server,port);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(name);
while (true) {
gui.output.append("\n"+in.readUTF());
}
} catch (Exception e) {
e.printStackTrace();
}
}

protected void sendTextToChat(String str) {
try {
out.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}

protected void disconnect() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main (String args[])throws IOException {
if (args.length!=3)
throw new RuntimeException ("Syntax: java AuctionClient <name> <serverhost> <port>");
int port=Integer.parseInt(args[2]);
AuctionClient c=new AuctionClient(args[0], args[1], port);
}
}

and class handler:


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

public class AuctionHandler extends Thread {

Socket socket;
DataInputStream in;
DataOutputStream out;
String name;
protected static Vector handlers = new Vector ();

public AuctionHandler (String name, Socket socket) throws IOException {

this.name = name;
this.socket = socket;

in = new DataInputStream (new BufferedInputStream (socket.getInputStream()));
out = new DataOutputStream (new BufferedOutputStream (socket.getOutputStream()));
}

public void run () {

try {

broadcast(name+" entered");
handlers.addElement (this);
while (true) {
String message = in.readUTF ();
broadcast(name+": "+message);
}

} catch (IOException ex) {
System.out.println("-- Connection to user lost.");
} finally {
handlers.removeElement (this);
broadcast(name+" left");
try {
socket.close ();
} catch (IOException ex) {
System.out.println("-- Socket to user already closed ?");
}
}
}

protected static void broadcast (String message) {
synchronized (handlers) {
Enumeration e = handlers.elements ();
while (e.hasMoreElements ()) {
AuctionHandler handler = (AuctionHandler) e.nextElement ();
try {
handler.out.writeUTF (message);
handler.out.flush ();
} catch (IOException ex) {
handler = null;
}
}
}
}
}


as Im using Gui for this Auction program now I don't know the code that 'member 42...' provided me where can be put in my codes to set timer first and then auctioning code??Confused | :confused:
GeneralRe: online auction using java client server socket?? Pin
Richard MacCutchan4-Nov-09 2:56
mveRichard MacCutchan4-Nov-09 2:56 
GeneralRe: online auction using java client server socket?? Pin
mica131419-Jul-10 17:35
mica131419-Jul-10 17:35 
GeneralRe: online auction using java client server socket?? Pin
Richard MacCutchan19-Jul-10 22:06
mveRichard MacCutchan19-Jul-10 22:06 
QuestionC++ to MySQL Pin
santhosh-padamatinti2-Nov-09 7:18
santhosh-padamatinti2-Nov-09 7:18 
AnswerRe: C++ to MySQL Pin
42774802-Nov-09 7:33
42774802-Nov-09 7:33 
Questionapplication reinstaller Pin
aviparida2-Nov-09 6:36
aviparida2-Nov-09 6:36 
AnswerRe: application reinstaller Pin
Richard MacCutchan2-Nov-09 6:57
mveRichard MacCutchan2-Nov-09 6:57 
AnswerRe: application reinstaller Pin
42774802-Nov-09 7:10
42774802-Nov-09 7:10 
AnswerRe: application reinstaller Pin
Nagy Vilmos2-Nov-09 21:58
professionalNagy Vilmos2-Nov-09 21:58 
GeneralRe: application reinstaller Pin
Richard MacCutchan2-Nov-09 22:10
mveRichard MacCutchan2-Nov-09 22:10 
Questionbutterfly graphics Pin
aviparida2-Nov-09 6:34
aviparida2-Nov-09 6:34 
AnswerRe: butterfly graphics Pin
42774802-Nov-09 7:12
42774802-Nov-09 7:12 
AnswerRe: butterfly graphics Pin
Nagy Vilmos2-Nov-09 22:00
professionalNagy Vilmos2-Nov-09 22:00 
GeneralRe: butterfly graphics Pin
Luc Pattyn3-Nov-09 1:07
sitebuilderLuc Pattyn3-Nov-09 1:07 
Question[Message Deleted] Pin
nGrafix2-Nov-09 3:22
nGrafix2-Nov-09 3:22 
AnswerRe: How to start Giutar Swing application in NetBeans Pin
Nagy Vilmos2-Nov-09 4:04
professionalNagy Vilmos2-Nov-09 4:04 
AnswerRe: How to start Giutar Swing application in NetBeans Pin
Richard MacCutchan2-Nov-09 5:06
mveRichard MacCutchan2-Nov-09 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.