Java download data book socket chat




















PrintWriter is itself the Bridge between byte level socket data and character form. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 7 months ago. Active 9 years, 7 months ago. Viewed 6k times.

So this is my client: import java. BufferedReader; import java. IOException; import java. InputStreamReader; import java. PrintWriter; import java. Socket; import javax. JAXBContext; import javax. JAXBException; import javax. Marshaller; import org. Logger; import dataart. StringReader; import java. Unmarshaller; import javax. StreamSource; import org. XMLProtocol; import dataart. Community Bot 1 1 1 silver badge.

Igor Masternoy Igor Masternoy 1 1 gold badge 10 10 silver badges 35 35 bronze badges. I want to read from iput stream in backgroud thread. Add a comment. Active Oldest Votes. It's commented out now, but it looks like your loop used to: - Read incoming messages from server - Read input from keyboard - Send input to server So you read whatever was available from the server, and then the client waits for more keyboard input before reading from the server again in the next iteration.

Did all like you say, and it work perfectly. One thread listening data from server, another listening data from user command line and post it to the server. I make console chat What problem with synchronization could exist on client side?

On Server side sending data to all clients is synchronized. I don't really see any synchronization issues for an application like this.

You're client isn't modifying any server-side state. This tutorial is divided between client side and server side development. Create a static Server socket at the beginning of main in the ChatServer class and instantiate it with a port number. TIP: ServerSockets may be instantiated with a port number other than , but numbers lower than are more likely to already be in use by the system.

Create a socket in the main method of Login, specifying the host address and port number. Since this is a login window, the socket can be an instance variable as it will be passed to the actual chat window later. TIP: "localhost" is used in the socket parameters for debugging purposes to specify that the server is running on the same computer as the client. Later this can be changed to take a host address to connect to a remote server. After instantiating the serverSocket in chatServer, start a while loop that continuously accepts all clients.

With the socket received from serversocket. Create a new Thread using the ClientThread, and then call start on the thread. In ClientThread make a private BufferedReader in to receive data from clients, and a PrintWriter to write to the client. Create a getter for the PrintWriter Initialize the PrintWriter in run with the socket's output stream, and the BufferedReader with a new InputStreamReader using the socket's input stream Create a while loop in run that checks for any new input and prints the input to all clients using the list of ClientThreads and the getter for the PrintWriter.

In ServerThread make a private BufferedReader in to receive data from the server, another BufferedReader to receive data from the user, and a PrintWriter to write to the server. In Create a while loop in run that checks for any new input from the server and prints the input to the console, and checks for any new input from the user and prints that input to the server.

After running the server and launching clients, the program should relay all messages from each client to all other clients. This was a small test of what can be done with java sockets.

You can expand on this project by adding functionality to let the server itself relay messages, archiving messages, allowing other forms of input and more to become more familiar with java sockets and networking in general.

Question 7 months ago on Step 8. Question 9 months ago. Does not work. Reply 1 year ago. Thankyou so much! I don't know what I could do without you. Reply 3 years ago. Question 2 years ago on Step 8. Question 2 years ago. Could you help me out, i have most of it down but how do you link the java files to the main one? And maybe you could give me the source code. Thanks, Isaac you can contact me at isaachenry gmail. Question 2 years ago on Step 3.



0コメント

  • 1000 / 1000