-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.m
More file actions
56 lines (46 loc) · 1.43 KB
/
client.m
File metadata and controls
56 lines (46 loc) · 1.43 KB
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
% CLIENT connect to a server and read a message
%
% Usage - message = client(host, port, number_of_retries)
function [person, message,input_socket] = client(host, port,input_socket, person,number_of_retries)
%person stays the same unless a new person is parsed
import java.net.Socket
import java.io.*
if (nargin < 5)
number_of_retries = 3; % set to -1 for infinite
end
retry = 0;
%person='';
message=[];
if isempty(input_socket)
while true
retry = retry + 1;
if ((number_of_retries > 0) && (retry > number_of_retries))
fprintf(1, 'Too many retries\n');
break;
end
try
fprintf(1, 'Try %d\n',retry);
% throws if unable to connect
input_socket = Socket(host, port);
break;
catch
% pause before retrying
pause(.2);%1
end
end
end
if ~isempty(input_socket)
try
message=receiveMessage(input_socket);
if ~isempty(message) && isletter(message(1,1))
person=message(1,1:20);
message=message(1,21:end);
person=strtrim(person);
end
catch err
input_socket.close;
rethrow(err)
end
end
person
end