Sample server

import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Server { public static void main(String args[]) throws IOException { String question, username, exit_line="/exit"; ServerSocket ser_socket = new ServerSocket(1742); Socket socket = ser_socket.accept(); Scanner sockIn = new Scanner(socket.getInputStream()); PrintStream sockOut= new PrintStream(socket.getOutputStream()); System.out.println("SERVER UP!"); username = sockIn.nextLine(); sockOut.print("Hello, " + username+ "!\n"); do{ question = sockIn.nextLine(); sockOut.print("Got it! \n"); }while (!(question.equals(exit_line))); socket.close(); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.