-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVoIPServer.java
136 lines (94 loc) · 3.01 KB
/
VoIPServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* VoIPServer.java
*
* Version:
* 1.0
*
* Revisions:
* 0
*/
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* Class used to create the instance of the server side of the VoIP
* application.
*
* @author Siddharth Kalluru
* @author Vijaykumar Chandrashekar
*/
public class VoIPServer{
int tcpPort, streamPort;
ServerSocket objServSock;
DatagramSocket objUDPSock;
/**
* Constructor to initialize the instance of VoIPServer
*
* @param servPort Port of Server to be used for chat and exchange
* of control messages
*
* @param streamPort Port to be used for streaming of voice
*/
public VoIPServer(int servPort, int streamPort) throws IOException{
this.tcpPort = servPort;
this.streamPort = streamPort;
//Create server obj and bind to a port
objServSock = new ServerSocket(tcpPort);
//Create a DatagramSocket for the streams
objUDPSock = new DatagramSocket(streamPort);
}
/**
* The main program.
*
* @param args Port Number to start the VoIP Server
*/
public static void main(String [] args){
if(args.length == 1){
try{
int servPort = Integer.parseInt(args[0]);
int streamPort = servPort+1;
try{
VoIPServer objVoIPSever = new VoIPServer(servPort, streamPort);
System.out.println("Server is waiting for connections on port: " + servPort);
Socket objSock = objVoIPSever.objServSock.accept();
System.out.println("Client connected.");
//TO DO: Make client multi-threaded, if needed
handleClient(objSock, objVoIPSever.objUDPSock);
}
catch(Exception e){
e.printStackTrace();
}
}
catch(NumberFormatException nfe){
System.out.println("NFE - Usage: Please run the server side application as follows\n"+
"java VoIPServer <port of server>");
}
}
else
System.out.println("Usage: Please run the server side application as follows\n"+
"java VoIPServer <port of server>");
}
/**
* A description of what the method does
*
* @param objSock A socket connection to the connected client
* @param objUDPSock A UDP socket to be used for streaming voice to
* the connected client
*
* @exception IOException
* @exception LineUnavailableException
* @exception ClassNotFoundException
* @exception UnsupportedAudioFileException
*/
public static void handleClient(Socket objSock, DatagramSocket objUDPSock) throws IOException,
LineUnavailableException,
UnsupportedAudioFileException, ClassNotFoundException{
HostUI clientUI = new HostUI("Server", objSock, objUDPSock,
objSock.getInetAddress(), objUDPSock.getLocalPort());
//Draw the UI
clientUI.drawUI("Connected to host.\n");
}
}