Skip to content
Snippets Groups Projects
Commit a5194767 authored by Peter's avatar Peter
Browse files

Started Assignment 2

parent f36fe905
No related branches found
No related tags found
No related merge requests found
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MV3500Cohort2018JulySeptember.homework2.Severson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JOptionPane;
/**
*
* @author Peter
*/
public class SeversonAssignment2_Client {
public final static String LOCALHOST = "0:0:0:0:0:0:0:1";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
while (true) {
System.out.println("TcpClient creating socket...");
int confirmButton = JOptionPane.OK_OPTION;
String message = "Are you sure";
JOptionPane.showConfirmDialog(null, message);
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes
// a connection to that IP in the form of the Socket
// object; the server uses a ServerSocket to wait for
// connections.
if (confirmButton == JOptionPane.OK_OPTION) {
Socket socket = new Socket(LOCALHOST, 2317); // locohost?
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Now we're talking!");
System.out.println("The message the server sent was " + serverMessage);
}
}
} catch (IOException e) {
System.out.println("Problem with client: ");
System.out.println(e);
}
System.out.println("client exit");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MV3500Cohort2018JulySeptember.homework2.Severson;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Peter
*/
public class SeversonAssingment2_Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
System.out.println("TcpServer has started..."); // it helps debugging to put this on console first
ServerSocket serverSocket = new ServerSocket(2317);
while (true) {
Socket clientConnection = serverSocket.accept(); // block until connected to a client
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This was written by the server"); // this goes back to client!
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 + " ))");
ps.flush();
clientConnection.close();
}
} catch (IOException e) {
System.out.println("problem with networking");
}
}
}
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