I am a novice programmer in java.
I have created a program which is similar to a chat application using socket programming.
I haven't used threads.
My question is:
Whenever a client1 wants to communicate to another client2 via the sever how can i know which client is communicating.
I thought if i could differentiate all clients from server side by using sessions it would be easier. Just a random thought.
why this question??
I want to know this because i have stored the adressess of clients in a file along with a sequence number to make the messages visible to the pair alone globally. Whenever a client sends a message it prefixes it with a sequence number which is got from the client1 client2 pair stored in that file.
Any help would be greatly appreciated. Im just a learner in java. I apologize for any obscurity in my question.
First of all, try to make a thread to open a socket because it helps in your GUI processing. And the point that you want to see client names can be done by this steps:-
Add an editbox and enter your name (client name) and send this name along with the message and at server-side, split message and name so that you can determine which message is sent by whom.
Ex:- Client name is Alex and the message is "hi" then the data which will be sent is something like "Alex+hi". Now split this message using split() function in java at the server-side.
Hope this encoding and decoding will help you.
Related
this is more a theoretical question and I hope this is the right place.
My intention is to write an API (or equal), someone can interact with to get a list of E-Mail adresses, which he wants to send a message to, without him seeing the Email in plain text (or being able to save it).
The other way around should work fine: someone sends a message and I send it to every E-Mail adress (E-Mail adresses invisible to caller), but the workload would be on my side.
Is this possible ?
I have a java code where I am making a tcp connection to a site. Then, after that, I want to pass the value of a variable so that the site will permanently display that value. For example, if I define int x = 10; in my java program, I want to pass this value to the site so that it echos that value for ever until a new value is passed. I have no idea how to do this however, is this even possible? Where should I look, in terms of both java and php? Thanks
I suppose, when you are creating a tcp-connection, means you are the client and the site you are connected to is the server. I also hope that you are the one in charge of the server here. If that is the case, then what you are looking for is a simple Java server-client system. This will get you started with that.
If, you are not in charge of the server then you'll just have to create the client part and send data. But that itself probably, won't be sufficient because the server should be able to parse your data. So, you'll have to find out in what format is the server expecting data from the client.
is it possible to send argument to java socket like we do in url argument passing
XXX.XXX.XXX.XXX:XXXXX?id=d1948b485d6
I have application which broadcast message from XXXX1 to XXXX10 port and every port i have assigned to specific user , so i want to restrict user from to access any other port .
XXX.XXX.XXX.XXX:XXXXX?id=d1948b485d6 by using unique id was decided to validate user with port,id buts its not possible so any other way do so.
That is a unique feature of web page access and can't be used with regular sockets. It can be used with servlets, though, because they are basically web pages.
Edit: Now that the question has been revised, I can now answer the underlying question much better than the original question. It is not a good idea to assign each user a port. Then you are limited to about 64000 users. Instead, the user should transmit authentication information with each request, or transmit it when opening the connection and then use only that connection.
I am writing an application on Android, and I want it to be able to know when the server (which I have written in C) dies or is shutdown. I am trying to implement this by having the server send a message to the client (the Android app) at a specified interval. If this message doesn't come, then the client knows that it is no longer connected to the server. They communicate using TCP sockets.
My problem is that the Android app cannot seem to read what the server writes. When it gets to the first part of the code where it tries to read from the server socket, it just hangs. In the Android app I am using a BufferedReader to read from the socket tmpstr = inFromServer.readLine(); where tmpstr is a string and inFromServer is the BufferedReader, and the C server is just using write write(newsockfd,"I got your message",18);.
I also tried using an alternative java server to see if my basic program logic was wrong. The test server used a PrintWriter, and it worked perfectly with the Android client.
What am I missing here? Is there a difference in the way Java and C buffer (or don't buffer) their data? If I need to give any additional information, please let me know.
Edit: I also have no trouble getting the C server to read data sent from the client. I only have trouble with getting the client to read data that is sent from the server.
Edit: The problem was fixed by adding a newline character (\n) to what the server sends to the client.
If I recall correctly, readLine() is not going to return the result until it has read a full line. It is going to block until then. You are not sending a full line from the C program.
Not only are you not sending a full line (\n terminated), but you also aren't even sending your entire C string since you're not sending the null terminator (Your C string actually contains 19 characters, not 18, if you include the null terminator). I don't recall what type of string format Java uses, if it's null terminated or not, but that part probably doesn't matter since it's looking for a \n, not a \0.
Try sending "I got your message\n" from the C server and let us know what happens then.
Oops, I just realized that the question had already been answered in the comments to it. Oh well. People really should post answers as answers.
hi every body could you please help me . I write java code for sending string msg between client and server using udp socket . but I want to to send real time voice so could you please give some notes to do it
I can point you a little of the way, you probably would want to use the Real-time Transport Protocol (RTP), which is more or less the standard for sending audio or video real time over the net. However the implementation is not straight forward, and you should use a helper library like jlibrtp for the implementation. There is also a RTP packetizer in Java Media Framework (JMF), but you don't wanna go there....
UDP has no quality of service guarantee, so when sending your packets of data you will need to add some sort of order number to your data to detremine how to put the data back together. For example you could send 3 datagram packets in order from the server, yet the client may get them in a different order (2,1,3). Or it may not get one of them at all, in which case you either want it resent (doubtful) or simply ignore it and move on at some timeout.
Look into using Real Time Protocol RFC3550 (http://en.wikipedia.org/wiki/Real-time_Transport_Protocol)
as the transport over UDP. RTCP as the control over TCP.