Results 1 to 2 of 2
- 11-13-2009, 08:33 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
a server program that needs some help :)
hello guys ;
i tried to write a simple server to send /receive strings concurrently through the same port .i used a class with thread which implements runnable.but the problem is the class which has runnable thread doesnt recognise socket object that i created in main class.(btw im akind of new at java so the terms i used as talking might be wrong) .i guess it will make sense when u see the program below. if i use different ports for sending and receiving and define new socket objects in each class will solve the problem but i wonder if there s any way to do that through the same port concurently.if i can work it out.id like to share the proper program ( both server and client codes) with u all. pls feel free to ask any question.i hope i could express whats wrong with the program.
best regards...
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.DataInput;
class MyThreaddd implements Runnable{
Thread t;
MyThreaddd () {
t = new Thread(this,"My thread");
t.start();
}
public void run() {
for(;;) {
//to receive date from client
Scanner sc=new Scanner(s.getInputStream());
String gelen=sc.nextLine();
System.out.println("client:"+gelen);
}
}
}
public class javaserverchat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss=new ServerSocket(25);
System.out.println("server is ready.."+InetAddress.getLocalHost());
Socket s=ss.accept();
System.out.println(s.getInetAddress()+" connected");
PrintStream pr=new PrintStream(s.getOutputStream());
pr.println("hello pal im server:) "); //to send string to the client
new MyThreaddd();
for(;;) {
// to send the string we type to the client
DataInputStream di=new DataInputStream(System.in);
try {
String okunan=di.readLine();
pr.println(okunan); //Client'a mesaj
//System.out.println(okunan);
//System.out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//---------------------------------
//s.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("port mesgul");
}
}
}
- 11-13-2009, 04:44 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
1. your socket is invisible because it is in a different class, it would be visible if you did something like this:
So the thread class has to be inside the chat class. There are other methods to do this but i think this is the easiest.Java Code:class Chat{ Socket s; class YourThread{} }
2 . Dont use such small port numbers like 25 use ports >2000 , because you dont want to use a port which may be taken by system.
3. I never used scanner for reading lines from , so i dont know if it works well, I rather use BufferedReader, but thats just my preference
4 There is no point in having two ports, you have a connection on one port, and from that connection you get input and output
Similar Threads
-
Multithreaded Client/Server Chat program
By f0ns in forum Threads and SynchronizationReplies: 3Last Post: 10-21-2009, 05:26 PM -
smtp server configuration with jboss server
By vilas_patil in forum Java ServletReplies: 0Last Post: 01-05-2009, 01:18 PM -
Chat program: how server distinguishes each client connected to it
By kathleenr in forum NetworkingReplies: 2Last Post: 12-27-2008, 08:02 PM -
[SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE
By jim478 in forum New To JavaReplies: 2Last Post: 07-28-2008, 07:32 PM -
When to use –client and -server option while running a java program
By Java Tip in forum java.langReplies: 0Last Post: 04-04-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks