Skip to content
Snippets Groups Projects
Commit ff5857bc authored by danielcain's avatar danielcain
Browse files

Assignment 2 tcpClient and tcpServer working

parent ee34f295
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2018JulySeptember.homework2;
import java.net.Socket;
import java.io.*;
import java.net.*;
/**
* credit to author mcgredo
*/
public class CainTcpClient {
public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1
public static void main(String[] args) {
try {
while (true) {
System.out.println("creating socket");
// * Changed IP from 2317 to 2468.
Socket socket = new Socket(LOCALHOST, 2468); // locohost?
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String inputMessage = "Client: We are under attack! What do we do? ";
System.out.println("Client: we told the server: We are under attack! What do we do? ");
String sendMessage = inputMessage + "\n";
bw.write(sendMessage);
bw.flush();
String input = br.readLine();
System.out.println("Client: copy, server: " + input);
// String serverMessage = br.readLine();
System.out.println("Client: We are on our way!\n" ); // + serverMessage
String update = br.readLine();
// String sendUpdate = update + "\n";
// System.out.println("Answer:" + sendUpdate);
} // end while(true)
} catch (IOException e) {
System.out.println("Problem with client: "); // describe what is happening
System.out.println(e);
}
// program exit: tell somebody about that
System.out.println("client exit");
}
}
package MV3500Cohort2018JulySeptember.homework2;
import java.io.*;
import java.net.*;
/**
*
* telnet localhost 2468
* telnet <ipOfServersLaptop> 2468
* credit to author mcgredo
*/
public class CainTcpServer
{
public static void main(String[] args)
{
try
{
// * Changed IP from 2317 to 2468.
ServerSocket serverSocket = new ServerSocket(2468);
int x = 1;
while(x<=10)
{
System.out.println("server here, starting loop #: " + x);
Socket clientConnection = serverSocket.accept(); // block until connected
InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String receivedInput = br.readLine();
System.out.println("Server: the client said: \n" + receivedInput);
String returnInput;
returnInput = "Get to the choppa!"; // shows up
System.out.println("Server: told the client to Get to the choppa!\n");
OutputStream os = clientConnection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
// bw.write(returnInput);
// System.out.println("Message volley");
PrintStream ps = new PrintStream(os);
ps.println("Get to the choppa!");
ps.println(returnInput);
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort();
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))");
// Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
ps.flush();
clientConnection.close();
x++;
}
}
catch(Exception e)
{
// cosmetic change to print line
System.out.println("You got problems! Deal with it");
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment